如何将JsonConvert.DeserializeObject两个数据

时间:2016-03-26 10:37:06

标签: c# json model building

    {
  "pagination": {
    "next_url": "https://api.instagram.com/v1/users/",
    "next_max_id": "98740505131"
  },
  "meta": {
    "code": 200
  },
  "data": [
    {
      "attribution": null,
      "videos": {
        "low_bandwidth": {
          "url": "https://scontent.cdninstagram.com/t50.2886-16/12787808_180458939000143_910172373_s.mp4",
          "width": 480,
          "height": 480
        },
        "standard_resolution": {
          "url": "https://scontent.cdninstagram.com/t50.2886-16/12824263_607003579446709_1011139465_n.mp4",
          "width": 640,
          "height": 640
        },
        "low_resolution": {
          "url": "https://scontent.cdninstagram.com/t50.2886-16/12787808_180458939000143_910172373_s.mp4",
          "width": 480,
          "height": 480
        }
      },
      "tags": [],
      "type": "video",
      "location": null,
      "comments": {
        "count": 2,
        "data": [
          {
            "created_time": 1457332172,
            "text": "什麼東西",
            "from": {
              "username": "d86241",
              "profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/11371189_421316874725117_327631552_a.jpg",
              "id": 397355082,
              "full_name": "Jhao-wei Hvang"
            },
            "id": 1200511729352353800
          },
          {
            "created_time": 1457771205,
            "text": "",
            "from": {
              "username": "realkikog",
              "profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/11820496_1860868347487361_262727492_a.jpg",
              "id": 530665716,
              "full_name": "K I K O"
            },
            "id": 1204194607797938400
          }
        ]
      },
      "filter": "Crema",
      "created_time": 1457326470,
      "link": "https://www.instagram.com/p/BCo546hPKpA/",
      "likes": {
        "count": 22,
        "data": [
          {
            "username": "ladyyihan",
            "profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/10684228_590857344404221_1064502415_a.jpg",
            "id": 38863087,
            "full_name": "Yihan"
          },
          {
            "username": "miding_cyh",
            "profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/10005160_844941595551352_2014300181_a.jpg",
            "id": 226855180,
            "full_name": "i米丁 Juri"
          },
          {
            "username": "aikoyin1985",
            "profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/12093346_523536977821971_1279823341_a.jpg",
            "id": 2228728531,
            "full_name": "aiko1985"
          },
          {
            "username": "sh1recheungg",
            "profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/927866_1722116411358109_618748252_a.jpg",
            "id": 416683725,
            "full_name": "Sh1reCheungg"
          }
        ]
      },
      "images": {
        "low_resolution": {
          "url": "https://scontent.cdninstagram.com/t51.2885-15/s320x320/e15/12446061_1590715141254039_2091776153_n.jpg?ig_cache_key=MTIwMDQ2MzkwMDQ3MDcxNjk5Mg%3D%3D.2",
          "width": 320,
          "height": 320
        },
        "thumbnail": {
          "url": "https://scontent.cdninstagram.com/t51.2885-15/s150x150/e15/12446061_1590715141254039_2091776153_n.jpg?ig_cache_key=MTIwMDQ2MzkwMDQ3MDcxNjk5Mg%3D%3D.2",
          "width": 150,
          "height": 150
        },
        "standard_resolution": {
          "url": "https://scontent.cdninstagram.com/t51.2885-15/e15/12446061_1590715141254039_2091776153_n.jpg?ig_cache_key=MTIwMDQ2MzkwMDQ3MDcxNjk5Mg%3D%3D.2",
          "width": 640,
          "height": 640
        }
      },
      "users_in_photo": [],
      "caption": {
        "created_time": 1457326470,
        "text": "測試東西一下",
        "from": {
          "username": "jiantai.cai",
          "profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/1515128_723721747740304_742397288_a.jpg",
          "id": 1417858881,
          "full_name": "Mars"
        },
        "id": 1200463904363030800
      },
      "user_has_liked": false,
      "id": "1200463900470716992_1417858881",
      "user": {
        "username": "jiantai.cai",
        "profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/1515128_723721747740304_742397288_a.jpg",
        "id": 1417858881,
        "full_name": "Mars"
      }
    }
  ]
}

我试试了 var collection = JsonConvert.DeserializeObject(json);

但评论数据总是为空

喜欢这个图片

enter image description here

我如何建模这个json

我想要

foreach(var item  in model){
data.comments.data.text
}

查看

请帮忙,

1 个答案:

答案 0 :(得分:1)

我正在使用Newtonsoft 8.0.2,我能够得到你所要求的东西。

string input = File.ReadAllText("C:\\Public\\input.json");
dynamic collection = JsonConvert.DeserializeObject(input);

foreach (var dataItem in collection.data)
{
    dynamic comments = dataItem.comments;

    foreach (dynamic comment in comments.data)
    {
        string text = comment.text;
    }
}

您缺少的是您没有遍历项目下的每个评论对象。