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

前端 未结 12 1190
旧巷少年郎
旧巷少年郎 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:08

    If you just need to get a few items from the JSON object, I would use Json.NET's LINQ to JSON JObject class. For example:

    JToken token = JObject.Parse(stringFullOfJson);
    
    int page = (int)token.SelectToken("page");
    int totalPages = (int)token.SelectToken("total_pages");
    

    I like this approach because you don't need to fully deserialize the JSON object. This comes in handy with APIs that can sometimes surprise you with missing object properties, like Twitter.

    Documentation: Serializing and Deserializing JSON with Json.NET and LINQ to JSON with Json.NET

提交回复
热议问题