C#Newton用子列表反序列化JSON

时间:2017-11-27 17:55:10

标签: c# serialization json.net

让我们说这是来自Facebook的API 回复(最后)。

到目前为止,我正在反序列化牛顿为每个获取id和created_time,但第二个包括反应,这是一个列表和一个注释元素,这是另一个列表。

我正在使用以下内容来浏览帖子:

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": ""
  }
}

谢谢!

1 个答案:

答案 0 :(得分:1)

您的课程可以通过以下方式构建:

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);
    }
}

请参阅此demo

希望这有帮助。