简体   繁体   中英

Iterate through Results array nested in a JSON

I've been trying many different things, but I cannot find a working solution how to find the number of results in an array which is nested in a JSON.

Here is the JSON:

{ 
   "Status":"OK",
   "ID":"xxx",
   "Results":[  ]
}

And this is the structure of the Results array:

[ 
   { 
      "Call":{ 
         "ID":1,
         "CustomerKey":null,
         "CustomerID":null
      },
      "PingID":4,
      "Key":null,
      "Properties":[ 
         { 
            "Name":"OrdinalID",
            "Value":"1"
         }
      ],
      "CreatedDate":"0001-01-01T00:00:00.000",
      "ID":0,
      "ObjectID":null
   }
]

I am trying to display results in a table. The table should have as many rows as there are results. But I am getting an incorrect number every time.

If I do JSON.length, I get 3, and if I do JSON.Results[0].length I get over 10, where the correct number of results/rows should be 4.

Any idea how to check the length of this?

The correct way is to do JSON.results.length . (assuming JSON is the object where you stored the result from parsing).

JSON.length will give you 3 because you get the number of properties in the original object which is 3.

JSON.results[0].length will give you the length of the first element of the array. The first element of the array is that object you posted 2nd, and this is why you get a high number.

You're looking for the length of the results array in that JSON object, hence JSON.results.length will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM