Javascript 获取json对象的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6756104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get size of json object
提问by nkcmr
i have a json object that gets returned by an AJAX request and I am having some trouble with the .length
because it keeps returning undefined
. Just wondering if I'm using it right:
我有一个由 AJAX 请求返回的 json 对象,我遇到了一些问题,.length
因为它一直在返回undefined
。只是想知道我是否正确使用它:
console.log(data.length);
console.log(data.phones.length);
They both return undefined
even though they are valid objects.
undefined
即使它们是有效对象,它们也会返回。
Update:
Sample of the JSON object returned:
更新:
返回的 JSON 对象示例:
{"reqStatus":true,"phones":{"one":{"number":"XXXXXXXXXX","type":"mobile"},"two":{"number":"XXXXXXXXXX","type":"mobile"}}}
回答by Kasun
You can use something like this
你可以使用这样的东西
<script type="text/javascript">
var myObject = {'name':'Kasun', 'address':'columbo','age': '29'}
var count = Object.keys(myObject).length;
console.log(count);
</script>
回答by tomfumb
Your problem is that your phones object doesn't have a length property (unless you define it somewhere in the JSON that you return) as objects aren't the same as arrays, even when used as associative arrays. If the phones object was an array it would have a length. You have two options (maybe more).
您的问题是您的手机对象没有 length 属性(除非您在返回的 JSON 中的某处定义它),因为对象与数组不同,即使用作关联数组也是如此。如果phones 对象是一个数组,它将有一个长度。您有两个选择(可能更多)。
Change your JSON structure (assuming this is possible) so that 'phones' becomes
"phones":[{"number":"XXXXXXXXXX","type":"mobile"},{"number":"XXXXXXXXXX","type":"mobile"}]
(note there is no word-numbered identifier for each phone as they are returned in a 0-indexed array). In this response
phones.length
will be valid.Iterate through the objects contained within your phones object and count them as you go, e.g.
var key, count = 0; for(key in data.phones) { if(data.phones.hasOwnProperty(key)) { count++; } }
更改您的 JSON 结构(假设这是可能的),以便 'phones' 变成
"phones":[{"number":"XXXXXXXXXX","type":"mobile"},{"number":"XXXXXXXXXX","type":"mobile"}]
(请注意,每个电话都没有字编号标识符,因为它们以 0 索引数组返回)。在此响应
phones.length
将是有效的。遍历您的手机对象中包含的对象并在您进行时计算它们,例如
var key, count = 0; for(key in data.phones) { if(data.phones.hasOwnProperty(key)) { count++; } }
If you're only targeting new browsers option 2 could look like this
如果您只针对新浏览器选项 2 可能如下所示
回答by ForestierSimon
Consider using underscore.js. It will allow you to check the size i.e. like that:
考虑使用underscore.js。它将允许您检查大小,即:
var data = {one : 1, two : 2, three : 3};
_.size(data);
//=> 3
_.keys(data);
//=> ["one", "two", "three"]
_.keys(data).length;
//=> 3
回答by kkk
you dont need to change your JSON format.
您不需要更改您的 JSON 格式。
replace:
代替:
console.log(data.phones.length);
with:
和:
console.log( Object.keys( data.phones ).length ) ;
回答by surya handoko
var json=[{"id":"431","code":"0.85.PSFR01215","price":"2457.77","volume":"23.0","total":"565.29"},{"id":"430","code":"0.85.PSFR00608","price":"1752.45","volume":"4.0","total":"70.1"},{"id":"429","code":"0.84.SMAB00060","price":"4147.5","volume":"2.0","total":"82.95"},{"id":"428","code":"0.84.SMAB00050","price":"4077.5","volume":"3.0","total":"122.32"}]
var obj = JSON.parse(json);
var length = Object.keys(obj).length; //you get length json result 4
回答by Hafsal Rh
try this
尝试这个
$.parseJSON(data).length
回答by Mahesh Gawhane
use this one
使用这个
//for getting length of object
int length = jsonObject.length();
or
或者
//for getting length of array
int length = jsonArray.length();
回答by user2674838
$(document).ready(function () {
$('#count_my_data').click(function () {
var count = 0;
while (true) {
try {
var v1 = mydata[count].TechnologyId.toString();
count = count + 1;
}
catch (e)
{ break; }
}
alert(count);
});
});