[英]C# Newton deserialize JSON with sub lists
假設這是來自Facebook的API json響應(最后)。
到目前為止,我正在反序列化newton json,以獲取每個ID的ID和created_time
,但是第二個包含的響應是一個列表,而comment元素是另一個列表。
我正在使用以下內容遍歷帖子:
var posts = JsonConvert.DeserializeObject<FacebookPostData>(fbData);
和
for each post in posts....
id = post.id
我是如何在循環中循環到發布Reactions
和發布Comments
。
到目前為止,我正在學習的課程是:
public class FacebookPostData
{
public List<FacebookPost> Data { get; set; }
}
public class FacebookPost
{
public string id { get; set; }
public string created_time { get; set; }
}
API響應為:
{
"data": [{
"id": "",
"created_time": ""
},
{
"id": "",
"created_time": "",
"reactions": {
"data": [{
"id": "",
"name": "",
"type": ""
},
{
"id": "",
"name": "",
"type": ""
}
],
"paging": {
"cursors": {
"before": "",
"after": ""
}
}
},
"comments": {
"data": [{
"created_time": "",
"from": {
"name": "",
"id": ""
},
"message": "",
"id": ""
}],
"paging": {
"cursors": {
"before": "",
"after": ""
}
}
}
}
],
"paging": {
"previous": "",
"next": ""
}
}
謝謝!
您的班級可以這樣構造:
public class FacebookPostData
{
public List<FacebookPost> data { get; set; }
public Paging3 paging { get; set; }
public FacebookPostData()
{
this.data = new List<FacebookPost>();
this.paging = new Paging3();
}
}
public class FacebookPost
{
public string id { get; set; }
public string created_time { get; set; }
public Reactions reactions { get; set; }
public Comments comments { get; set; }
public FacebookPost()
{
this.reactions = new Reactions();
this.comments = new Comments();
}
}
public class Paging3
{
public string previous { get; set; }
public string next { get; set; }
}
public class Reactions
{
public List<Data2> data { get; set; }
public Paging paging { get; set; }
public Reactions()
{
this.data = new List<Data2>();
this.paging = new Paging();
}
}
public class Data2
{
public string id { get; set; }
public string name { get; set; }
public string type { get; set; }
}
public class Paging
{
public Cursors cursors { get; set; }
public Paging()
{
this.cursors = new Cursors();
}
}
public class Cursors
{
public string before { get; set; }
public string after { get; set; }
}
public class Comments
{
public List<Data3> data { get; set; }
public Paging2 paging { get; set; }
public Comments()
{
this.data = new List<Data3>();
this.paging = new Paging2();
}
}
public class Data3
{
public string created_time { get; set; }
public From from { get; set; }
public string message { get; set; }
public string id { get; set; }
public Data3()
{
this.from = new From();
}
}
public class Paging2
{
public Cursors2 cursors { get; set; }
public Paging2()
{
this.cursors = new Cursors2();
}
}
public class From
{
public string name { get; set; }
public string id { get; set; }
}
public class Cursors2
{
public string before { get; set; }
public string after { get; set; }
}
因此,您可以執行以下操作:
var posts = JsonConvert.DeserializeObject<FacebookPostData>(fbData);
foreach(var post in posts.data)
{
Console.WriteLine(post.id);
// Reactions...
foreach(var reaction in post.reactions.data)
{
Console.WriteLine(reaction.id);
}
// Comments...
foreach(var comment in post.comments.data)
{
Console.WriteLine(comment.id);
Console.WriteLine(comment.from.id);
Console.WriteLine(comment.from.name);
}
}
看到這個演示 。
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.