简体   繁体   中英

Deserializing an array with json.net

I need to deserialize a json array:

{"response":
[19,
    {"mid":1068,
     "date":1343848664,
     "out":1,
     "uid":44852633,
     "read_state":1,
     "title":" ... ",
     "body":"А я вовсю."},
    {"mid":1007,
     "date":1328782448,
     "out":1,
     "uid":16098752,
     "read_state":0,
     "title":" ... ",
     "body":"http:\/\/theantidj.net\/wp-content\/themes\/theantidjnet\/images\/khubvio.php"}

and so on. I'm using json.net. Can I perform deserealization using JsonConvert.DeserializeObject()? And if so what classes should I create?

Just another solution.

var itemList = ((JObject)JsonConvert.DeserializeObject(json))["response"]
                .Skip(1)
                .Select(x => JsonConvert.DeserializeObject<Item>(x.ToString()))
                .ToList();


public class Item
{
    public int mid { set; get; }
    public string date { set; get; }
    public int @out { set; get; }
    public int  uid { set; get; }
    public int read_state { set; get; }
    public string title { set; get; }
    public string body { set; get; }
}

LB provided the solution. But i would notice if my memory serves me well that JObject is used for dynamic types. Thus it requires .Net 4 or higher. Just in case.

You can use JsonConvert.DeserializeObject<RootObject>(jsonstring) after you define this RootObject .

The tool I use is http://json2csharp.com where you simply put in JSON and get out a corresponding object. You will want to make sure your JSON is well formed though, since in its current form it is not parsing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM