4

ive got a problem getting parameters from a json chain, the json I got looks something like this

[{"aa":"bb","ccc":"ddd","eeee":"ffff","ggggg":"hhhhh","iiiiii":"jjjjjj","kkkkkkk":"lllllll"}]

Im trying to count how many pairs there are inside the '{}' but i dont know how. I tried json.length and json[0].length, the first one gave me back the value '1' and the second one undefined.

4

1 回答 1

2

您的 json 对象是一个包含一个对象的数组。所以长度为1。数组中的对象有多个属性(键/值对)。

因此,在大多数现代浏览器(IE 除外)中,这对您有用:

Object.keys(json[0]).length

在此处查看用于在 JavaScript 中迭代/计算对象属性的各种技术的答案:

于 2012-11-03T17:32:26.390 回答