使用JSON.NET将Facebook提要从JSON转换为类型对象

时间:2014-05-28 13:42:01

标签: c# json facebook windows-phone-8 json.net

问题是,我从facebook(页面提要)收到一个帖子列表,偶尔会在post对象内的数组中保存多个注释。只有帖子的第一条评论才能正确解析。不知怎的,第二个和后面的评论都没有得到解析。

这是我正在谈论的JSON:

{
  "data": [ //Root object
  { //Post object
     "id": "153411918116843_304953306296036",
     "from": {
        "category": "Sports/recreation/activities",
        "category_list": [
           {
              "id": "124887510918208",
              "name": "Swimming Pool"
           }
        ],
        "name": "Twentebad",
        "id": "153411918116843"
     },
     "link": "http://dlvr.it/5bBKPx",
     "picture": "irrelevant",
     "message": "Onderzoek renovatie Twentebad! http://dlvr.it/5bBKPx",
     "created_time": "2014-05-07T09:44:18+0000",
     "comments": { // Collective comments object


        // Actual comments objects in array !!!!! ONLY FIRST GETS PARSED !!!!
        "data": [
           { //Comment object
              "id": "304953306296036_304959069628793",
              "from": {
                 "id": "884483928243965",
                 "name": "Selena Suzan Valentino"
              },
              "message": "Twentepalace",
              "can_remove": false,
              "created_time": "2014-05-07T10:32:26+0000",
              "like_count": 0,
              "user_likes": false
           },
           { Unparsed comment object
              "id": "304953306296036_305126702945363",
              "from": {
                 "id": "884483928243965",
                 "name": "Selena Suzan Valentino"
              },
              "message": "Na baantjes trekken vooral heerlijk...",
              "can_remove": false,
              "created_time": "2014-05-08T09:12:43+0000",
              "like_count": 0,
              "user_likes": false
           },
           { //Unparsed comment object
              "id": "304953306296036_305126622945371",
              "from": {
                 "id": "884483928243965",
                 "name": "Selena Suzan Valentino"
              },
              "message": "Wil infrarood sauna weer terug komt...",
              "can_remove": false,
              "created_time": "2014-05-08T09:11:20+0000",
              "like_count": 0,
              "user_likes": false
           }
        ],



        "paging": {
           "cursors": {
              "after": "MQ==",
              "before": "Mw=="
           }
        },
        "summary": {
           "order": "ranked",
           "total_count": 3
        }
     }
  }
  ]
}

我正在使用JSON.NET将这个json解析为这样的类型化对象列表:

foreach (JToken facebookItem in data["data"])
{
    JToken messageCheck = facebookItem.SelectToken("message", false); //Irrelevante post als er geen inhoud in zit (false = geen error melding indien null)
    if (messageCheck != null)
    {
        itemsToAdd.Add(facebookItem.ToObject<FacebookItem>());
    }
}

FacebookItem类看起来像这样:

public class FacebookItem
{
    public string id { get; set; }
    public From from { get; set; }
    public string link { get; set; }
    public string picture { get; set; }
    public string message { get; set; }
    public string created_time { get; set; }
    public Comments comments { get; set; }
    public PostPaging paging { get; set; }

    public class PostCategories
    {
        public string id { get; set; }
        public string name { get; set; }
    }

    public class From
    {
        public string category { get; set; }
        public List<PostCategories> category_list { get; set; }
        public string name { get; set; }
        public string id { get; set; }
    }

    public class CommentCategories
    {
        public string id { get; set; }
        public string name { get; set; }
    }

    //Root of Comments ("data":[ "comments":{ [],{},{} } ])
    public class Comments
    {
        public List<CommentData> data { get; set; } //SHOULD HOLD MORE THAN 1
        public CommentPaging paging { get; set; }
        public Summary summary { get; set; }
    }

    //Data gegevens van comments ("data":[ "comments":{ "data":[],{},{} } ])
    public class CommentData
    {
        public string id { get; set; }
        public CommentFrom from { get; set; }
        public string message { get; set; }
        public bool can_remove { get; set; }
        public string created_time { get; set; }
        public int like_count { get; set; }
        public bool user_likes { get; set; }
    }

    //Data gegevens van ccommenter ("data":[ "comments":{ "data":[ "from":{} ],{},{} } ])
    public class CommentFrom
    {
        public string id { get; set; }
        public string name { get; set; }
        public string category { get; set; }
        public List<CommentCategories> category_list { get; set; }
    }

    //Paging gegevens van comments ("data":[ "comments":{ [],"paging":{},{} } ])
    public class CommentPaging
    {
        public Cursors cursors { get; set; }
    }

    //Summary gegevens van comments ("data":[ "comments":{ [],{},"summary":{} } ])
    public class Summary
    {
        public string order { get; set; }
        public int total_count { get; set; }
    }

    public class Cursors
    {
        public string after { get; set; }
        public string before { get; set; }
    }

    public class PostPaging
    {
        public string previous { get; set; }
        public string next { get; set; }
    }
}

有没有一个Json.net专家,为什么只有第一个评论被解析? 这是调试器给我看的:

Debugger breakpoint

1 个答案:

答案 0 :(得分:1)

我会告诉你我住的地方,请来杀我......

我从localstorage中调用了url。我更改了网址,但未检测到更改,因此无法覆盖。

那么,为什么只有一条评论呢?我有评论&#39;限制设为1 :(

不像我希望的那样退出。 希望所提供的信息可以在将来帮助某人。