简体   繁体   English

使用json.net反序列化数组

[英]Deserializing an array with json.net

I need to deserialize a json array: 我需要反序列化json数组:

{"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. 我正在使用json.net。 Can I perform deserealization using JsonConvert.DeserializeObject()? 我可以使用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. LB提供了解决方案。 But i would notice if my memory serves me well that JObject is used for dynamic types. 但是我会注意到,如果我的记忆能很好地为我使用JObject用于动态类型。 Thus it requires .Net 4 or higher. 因此,它需要.Net 4或更高版本。 Just in case. 以防万一。

You can use JsonConvert.DeserializeObject<RootObject>(jsonstring) after you define this RootObject . 定义此RootObject后,可以使用JsonConvert.DeserializeObject<RootObject>(jsonstring)

The tool I use is http://json2csharp.com where you simply put in JSON and get out a corresponding object. 我使用的工具是http://json2csharp.com ,您只需在其中放入JSON并获得相应的对象。 You will want to make sure your JSON is well formed though, since in its current form it is not parsing. 但是,您将要确保JSON格式正确,因为它目前没有解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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