JSON对象不支持length属性。如果想得到一个JSON包含的最顶层的对象的数目,必须用
for 函数来遍历
一种是单纯的数组:
strJSON = "['one','two',{obj1:'obj1_one',obj2:'obj2_two'}]";
var arrObj = eval('(' +strJSON+ ')');
alert(arrObj.length);
另外一种是复杂对象:
var jsonString = "{field1:'data1',field2:'data2',arrField1:['arrField1Content1','arrField1Content2']}";
var obj = eval('(' + jsonString + ')');
var objCount=0;
for(_obj in obj) objCount++;
alert(objCount);