c#解析嵌套json

本文关键字:json 嵌套 | 更新日期: 2023-09-27 18:13:27

我想知道如何从json中获得数组元素。我使用JSON。. NET和已经访问json文件,但不能弄清楚如何解析嵌套数组元素,如节点

JSON是这样的:

    {
      "language_code": "en",
      "platform": "web",
      "entry_data": {
        "ProfilePage": [
          {
            "user": {
              "country_block": null,
              "id": "3237234838",
              "full_name": "Lobi",
              "media": {
                "count": 217,
                "nodes": [
                  {
                    "code": "BLmDO3qAhtB",
                    "date": 1476558058,
                    "dimensions": {
                      "width": 1080,
                      "height": 720
                    },
                    "comments_disabled": false,
                    "comments": {
                      "count": 7
                    },
                    "caption": "Don't limit yourself by the ideas others put into your head! Try something different, think different, life different - simply be more than the day before!   Send some love    Double tap! Like 3 posts!'n'nFollow    @pollux.starchild 'nFollow    @pollux.starchild 'nEveryday adventures: @sundownshots",
                    "likes": {
                      "count": 196
                    },
                    "owner": {
                      "id": "3237234838"
                    },
                    "thumbnail_src": "https://igcdn-photos-a-a.akamaihd.net/hphotos-ak-xpa1/t51.2885-15/s640x640/sh0.08/e35/c180.0.720.720/14714497_117085415427760_4015602307872849920_n.jpg?ig_cache_key=MTM2MTc5MDE1MzI5ODk0Mjc4NQ%3D%3D.2.c",
                    "is_video": false,
                    "id": "1361790153298942785",
                    "display_src": "https://igcdn-photos-a-a.akamaihd.net/hphotos-ak-xpa1/t51.2885-15/e35/14714497_117085415427760_4015602307872849920_n.jpg?ig_cache_key=MTM2MTc5MDE1MzI5ODk0Mjc4NQ%3D%3D.2"
                  },
                  {
                    "code": "BLjealpA06h",
                    "date": 1476471646,
                    "dimensions": {
                      "width": 1080,
                      "height": 720
                    },
                    "comments_disabled": false,
                    "comments": {
                      "count": 7
                    },
                    "caption": "New Beginnings are mostly scary initially, but they can take many pleasant unexpected turns!   Send some love    Double tap! Like 3 posts!'n'nFollow    @pollux.starchild 'nFollow    @pollux.starchild 'nEveryday adventures: @sundownshots",
                    "likes": {
                      "count": 236
                    },
                    "owner": {
                      "id": "3237234838"
                    },
                    "thumbnail_src": "https://igcdn-photos-g-a.akamaihd.net/hphotos-ak-xpa1/t51.2885-15/s640x640/sh0.08/e35/c180.0.720.720/14704974_741258352693470_5618822833925783552_n.jpg?ig_cache_key=MTM2MTA2NTI4MDkxNDI4MDA5Nw%3D%3D.2.c",
                    "is_video": false,
                    "id": "1361065280914280097",
                    "display_src": "https://igcdn-photos-g-a.akamaihd.net/hphotos-ak-xpa1/t51.2885-15/e35/14704974_741258352693470_5618822833925783552_n.jpg?ig_cache_key=MTM2MTA2NTI4MDkxNDI4MDA5Nw%3D%3D.2"
                  },
                  {
                    "code": "BLg5w9QB3G4",
                    "date": 1476385322,
                    "dimensions": {
                      "width": 1080,
                      "height": 720
                    },
                    "comments_disabled": false,
                    "comments": {
                      "count": 4
                    },
                    "caption": "Be open to turn your life upside down for good!   Send some love    Double tap! Like 3 posts!'n'nFollow    @pollux.starchild 'nFollow    @pollux.starchild 'nEveryday adventures: @sundownshots",
                    "likes": {
                      "count": 242
                    },
                    "owner": {
                      "id": "3237234838"
                    },
                    "thumbnail_src": "https://igcdn-photos-g-a.akamaihd.net/hphotos-ak-xpa1/t51.2885-15/s640x640/sh0.08/e35/c180.0.720.720/14701136_922253567919054_2476090857082585088_n.jpg?ig_cache_key=MTM2MDM0MTE0MDQxOTA4MDYzMg%3D%3D.2.c",
                    "is_video": false,
                    "id": "1360341140419080632",
                    "display_src": "https://igcdn-photos-g-a.akamaihd.net/hphotos-ak-xpa1/t51.2885-15/e35/14701136_922253567919054_2476090857082585088_n.jpg?ig_cache_key=MTM2MDM0MTE0MDQxOTA4MDYzMg%3D%3D.2"
                  },
                  {
                    "code": "BLeTTxGhv2O",
                    "date": 1476298051,
                    "dimensions": {
                      "width": 1080,
                      "height": 720
                    },
                    "comments_disabled": false,
                    "comments": {
                      "count": 7
                    },
                    "caption": "I bet you have something within you, you wish it was real. Fight for it and don't let the world out there change you! Only change to be better than yesterday.   Send some love    Double tap! Like 3 posts!'n'nFollow    @pollux.starchild 'nFollow    @pollux.starchild 'nEveryday adventures: @sundownshots",
                    "likes": {
                      "count": 258
                    },
                    "owner": {
                      "id": "3237234838"
                    },
                    "thumbnail_src": "https://igcdn-photos-g-a.akamaihd.net/hphotos-ak-xpa1/t51.2885-15/s640x640/sh0.08/e35/c180.0.720.720/14711876_767095436764222_3395160437623357440_n.jpg?ig_cache_key=MTM1OTYwOTA1ODc4OTA5Njg0Ng%3D%3D.2.c",
                    "is_video": false,
                    "id": "1359609058789096846",
                    "display_src": "https://igcdn-photos-g-a.akamaihd.net/hphotos-ak-xpa1/t51.2885-15/e35/14711876_767095436764222_3395160437623357440_n.jpg?ig_cache_key=MTM1OTYwOTA1ODc4OTA5Njg0Ng%3D%3D.2"
                  },
                  {
                    "code": "BLbvkEnAybC",
                    "date": 1476212202,
                    "dimensions": {
                      "width": 1080,
                      "height": 720
                    },
                    "comments_disabled": false,
                    "comments": {
                      "count": 11
                    },
                    "caption": "It's not about weakness but emotional exclusiveness.   Send some love    Double tap! Like 3 posts!'n'nFollow    @pollux.starchild 'nFollow    @pollux.starchild 'nEveryday adventures: @sundownshots",
                    "likes": {
                      "count": 248
                    },
                    "owner": {
                      "id": "3237234838"
                    },
                    "thumbnail_src": "https://igcdn-photos-f-a.akamaihd.net/hphotos-ak-xpa1/t51.2885-15/s640x640/sh0.08/e35/c180.0.720.720/14540408_1108129005944901_8031323788203261952_n.jpg?ig_cache_key=MTM1ODg4ODg5OTYxOTAwNjE0Ng%3D%3D.2.c",
                    "is_video": false,
                    "id": "1358888899619006146",
                    "display_src": "https://igcdn-photos-f-a.akamaihd.net/hphotos-ak-xpa1/t51.2885-15/e35/14540408_1108129005944901_8031323788203261952_n.jpg?ig_cache_key=MTM1ODg4ODg5OTYxOTAwNjE0Ng%3D%3D.2"
                  },
                  {
                    "code": "BLMSCq2hSFD",
                    "date": 1475693407,
                    "dimensions": {
                      "width": 1080,
                      "height": 608
                    },
                    "comments_disabled": false,
                    "comments": {
                      "count": 12
                    },
                    "caption": "Hold on to the people who care for you! Look for actions, not for words...   Send some love    Double tap! Like 3 posts!'n'nFollow    @pollux.starchild 'nFollow    @pollux.starchild 'nEveryday adventures: @sundownshots",
                    "likes": {
                      "count": 277
                    },
                    "owner": {
                      "id": "3237234838"
                    },
                    "thumbnail_src": "https://igcdn-photos-b-a.akamaihd.net/hphotos-ak-xpa1/t51.2885-15/e35/c236.0.608.608/14449294_311761075847057_5957362236484747264_n.jpg?ig_cache_key=MTM1NDUzNjkzNjIxOTY4MTA5MQ%3D%3D.2.c",
                    "is_video": false,
                    "id": "1354536936219681091",
                    "display_src": "https://igcdn-photos-b-a.akamaihd.net/hphotos-ak-xpa1/t51.2885-15/e35/14449294_311761075847057_5957362236484747264_n.jpg?ig_cache_key=MTM1NDUzNjkzNjIxOTY4MTA5MQ%3D%3D.2"
                  }
                ]
              },
              "is_verified": false,
              "external_url": null
            }
          }
        ]
      }
    }

  JObject jResults = JObject.Parse(fullstring);
                var language_code = jResults["language_code"].Value<string>();

提前感谢!

c#解析嵌套json

如果它是数组,那么您需要使用index来确定在这个数组中需要哪个元素

例如:

var userId = jResults["entry_data"]["ProfilePage"][0]["user"]["id"].ToString()