我遇到了json数组行长的问题......
我想提取正确长度的数据行json(在这种情况下" 2")... 如果我试着计算长度,我会得到值" 1"。为什么?我怎样才能得到正确的长度?
{
"data-row": {
"data1": [{
"firstName": "John",
"lastName": "Doe"
}, {
"firstName": "Anna",
"lastName": "Smith"
}, {
"firstName": "Peter",
"lastName": "Jones"
}],
"data2": [{
"firstName": "John",
"lastName": "Doe"
}, {
"firstName": "Anna",
"lastName": "Smith"
}]
}
}
答案 0 :(得分:3)
您有一个Object
并且找到Object
var json = {
"data-row": {
"data1": [],
"data2": []
}
}
alert(Object.keys(json["data-row"]).length);
当您-
key
使用json["data-row"]
访问object
答案 1 :(得分:1)
这是查找长度的另一种方法。但不是正确的方式。
var json = {
"data-row": {
"data1": [],
"data2": []
}
}
length=0;
$.each(json["data-row"],function(i){
length++;
});
alert(length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>