Deserializing JSON to .NET object using Newtonsoft (or LINQ to JSON maybe?)

前端 未结 12 1202
旧巷少年郎
旧巷少年郎 2020-11-22 10:04

I know there are a few posts about Newtonsoft so hopefully this isn\'t exactly a repeat...I\'m trying to convert JSON data returned by Kazaa\'s API into a nice object of som

12条回答
  •  深忆病人
    2020-11-22 10:05

    If, like me, you prefer to deal with strongly typed objects** go with:

    MyObj obj =  JsonConvert.DeserializeObject(jsonString);
    

    This way you get to use intellisense and compile time type error checking.

    You can easily create the required objects by copying your JSON into memory and pasting it as JSON objects (Visual Studio -> Edit -> Paste Special -> Paste JSON as Classes).

    See here if you don't have that option in Visual Studio.

    You will also need to make sure your JSON is valid. Add your own object at the start if it is just an array of objects. i.e. {"obj":[{},{},{}]}

    ** I know that dynamic makes things easier sometimes but I'm a bit ol'skool with this.

提交回复
热议问题