0
How can i found the length of JSON array..?
i have an array of Json Objects ...
2 ответов
+ 13
you can use the Object.keys() prototype function to get an array of the keys and then use length property of the array
var myObj = {
a:1,
b:2,
c:3
}
var size = Object.keys(myObj).length;
this should produce 3
+ 5
If you are meaning "how to get the length of an array encoded as JSON string without parsing it", it's quite inefficient and should require minimal parsing of the string to count the item listed... but think that you will get the result slowest than by juste parsing the JSON string and accessing to the real object length property, as well as you will consume less memory (you will at least pass the string as argument to a custom function, and by so automatically make a temporary copy of it: but any encoded data as text will always consume more memory than the binary model).
Also, parsing brackets and quotes is required to analyze and count the items in the array JSON string encoded: this is not impossible at all, and could be a good exercise/challenge, but it's also not the most obvious task ;) (I've write a minimal version I could publish/post if anybody want -- inefficient comparing to a simple JSON.parse(str).length ;P)