2021年4月29日星期四

C# How to get JSON multiple level nested of multiple same name values

I'm trying to pull out some JSON data values from a JSON response. I get an array of JSON data and have gotten the parse to JArray to work and get the values of the JProperties that are not nested. I can't figure out how to get the "id" data out of a property that has nested info in it for categories, components, and capabilities. The "id" data count is not the same for each of the items.

"items": [  {    "deviceId": "DeviceNumber", <-Can get these values OK    "name": "DeviceName",    "label": "DeviceLabel",    "locationId": "LocationId",    "components": [      {        "id": "main", <-NEED THIS        "label": "DeviceLabel",        "capabilities": [          {            "id": "ID1", <-NEED THIS            "version": 1 <-NEED THIS          },          {            "id": "ID2", <-NEED THIS            "version": 1 <-NEED THIS          },          {            "id": "ID3", <-NEED THIS            "version": 1 <-NEED THIS          }        ],        "categories": [          {            "name": "CategoryName", <-NEED THIS            "categoryType": "manufacturer"          }        ]      }    ],    "dth": {      "deviceTypeId": "deviceID"    },    "type": "typeID"  },  ...Next item with same info  

Here is the code I have to get the "deviceId" and the other items that are next to it before components.

        JObject parsed = JObject.Parse(deviceListJSONstring);          JArray array = (JArray)parsed["items"];          foreach (JObject item in array.Children<JObject>())          {              foreach (JProperty property in item.Properties())              {                    if (property.Name.Equals("deviceId"))                  {                      Console.WriteLine(property.Value);                      deviceID = property.Value.ToString();                  }                  else if (property.Name.Equals("components"))                  {                  #What needs to be here?                  }  

Any assistance would be appreciated.

https://stackoverflow.com/questions/67303963/c-sharp-how-to-get-json-multiple-level-nested-of-multiple-same-name-values April 29, 2021 at 12:13AM

没有评论:

发表评论