我正在使用JSON.Net尝试反序列化来自facebook的一些响应。以下是我正在阅读的数据的快照:
{
"data": [
{
"id": "123"
},
{
"id": "234"
}
],
"paging": {
"cursors": {
"before": "xxx",
"after": "xxx"
},
"next": "xxx"
}
}
类:
class jsonDeserialize
{
public List<ListDetail> ListDetail { get; set; }
}
public class DataList
{
public string id { get; set; }
}
public class Paging
{
public List<string> cursors { get; set; }
public string next { get; set; }
}
public class Cursors
{
public string before { get; set; }
public string after { get; set; }
}
public class ListDetail
{
public List<Cursors> Cursors { get; set; }
public List<Paging> Articles { get; set; }
public List<DataList> DataList { get; set; }
}
我使用以下代码来使用JSON.NET反序列化函数:由于某种原因,结果返回null,请帮助我:(
var result = JsonConvert.DeserializeObject<jsonDeserialize>(jsonString);
修复类防御:
class jsonDeserialize
{
public List<DataList> data { get; set; }
public Paging paging { get; set; }
}
public class DataList
{
public string id { get; set; }
}
public class Paging
{
public Cursors cursors { get; set; }
public string next { get; set; }
}
public class Cursors
{
public string before { get; set; }
public string after { get; set; }
}
jsonDeserialize
包含DataList的列表,ListDetail
可以删除,cursors
不是数组,它是一个对象。
Omg,我错过了它是来自facebook的JSON,并"解决"了反向问题。不过,我会把它放在这里,以防万一
首先,作为一个简短的注释,这里的反序列化代码必须看起来像var result = JsonConvert.DeserializeObject
<jsonDeserialize>
(jsonString);
您的JSON与代码不对应。如果你想反序列化到jsonDeserialize类,你的json必须是这样的(关注[]
和{}
(
{
"ListDetail" :
[
{
"Cursors":
[
{
"before" : "value",
"after" : "value"
}
],
"Articles":
[
{
"Cursors":
[
"cursor1",
"cursor2"
],
"next" : "somenext"
}
],
"DataList":
[
{
"id" : "someid"
},
{
"id" : "someid2"
}
]
}
]
}
class jsonDeserialize
{
public List<DataList> data { get; set; }
public Paging paging { get; set; }
}
public class DataList
{
public string id { get; set; }
}
public class Paging
{
public Cursors cursors { get; set; }
public string next { get; set; }
}
public class Cursors
{
public string before { get; set; }
public string after { get; set; }
}
var result=JsonConvert.DeserializeObject<jsonDeserialize>(jsonformat.Substring(0,jsonformat.Length));
使用它会给你写结果。beacuse数据和分页被视为属性,因此您需要在类jsondeseailized中初始化这些属性,而不是列表使用游标,因为您将游标数据存储在分页类中。感谢