admin管理员组文章数量:1127998
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:
console.log(data.length);
console.log(data.phones.length);
They both return undefined
even though they are valid objects.
Update:
Sample of the JSON object returned:
{"reqStatus":true,"phones":{"one":{"number":"XXXXXXXXXX","type":"mobile"},"two":{"number":"XXXXXXXXXX","type":"mobile"}}}
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:
console.log(data.length);
console.log(data.phones.length);
They both return undefined
even though they are valid objects.
Update:
Sample of the JSON object returned:
{"reqStatus":true,"phones":{"one":{"number":"XXXXXXXXXX","type":"mobile"},"two":{"number":"XXXXXXXXXX","type":"mobile"}}}
Share
Improve this question
edited Apr 1, 2021 at 16:17
Syscall
19.8k10 gold badges43 silver badges57 bronze badges
asked Jul 20, 2011 at 1:44
nkcmrnkcmr
11k25 gold badges68 silver badges87 bronze badges
3
|
9 Answers
Reset to default 717You can use something like this
var myObject = {'name':'Kasun', 'address':'columbo','age': '29'}
var count = Object.keys(myObject).length;
console.log(count);
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).
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++; } }
If you're only targeting new browsers option 2 could look like this
you dont need to change your JSON format.
replace:
console.log(data.phones.length);
with:
console.log( Object.keys( data.phones ).length ) ;
Consider using underscore.js. It will allow you to check the size i.e. like that:
var data = {one : 1, two : 2, three : 3};
_.size(data);
//=> 3
_.keys(data);
//=> ["one", "two", "three"]
_.keys(data).length;
//=> 3
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
try this
$.parseJSON(data).length
Use this one
Object.keys(jsonObject).length
var data = {"reqStatus":true,"phones":{"one":{"number":"XXXXXXXXXX","type":"mobile"},"two":{"number":"XXXXXXXXXX","type":"mobile"}}}
console.log(Object.keys(data).length);
console.log(Object.keys(data.phones).length);
console.log(Object.keys(data.phones.two).length);
$(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);
});
});
本文标签: javascriptget size of JSON objectStack Overflow
版权声明:本文标题:javascript - get size of JSON object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736709002a1948835.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
.lenght
instead of.length
and typescript interpreter never complained. Welcome to scripting world! – Atul Commented Feb 8, 2022 at 18:29