I have a string st
returned from a web service. I convert the string to an object, how can I count the no. of arrays inside it? (for this case it's 2)
var st = "{[{"Name": "fake", "Address": "add"]},[{"Name": "fake", "Address": "add"}]}";
var json = eval(st);
json.length
is always returning 1
@coure06 I`ve changed a little bit your JSON. Hope this will be usefull
var persons = {
"person1" : {
"Name": "Adam",
"Address": "USA"
},
"person2" : {
"Name": "Jamie",
"Address": "USA"
}
};
var count = 0;
//var count = persons.length; // wont work
for ( property in persons ) // should return 2
{
if(persons.hasOwnProperty(property))
{
count++;
}
}
Live example at jsfiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With