Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jSon objects count

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

like image 699
coure2011 Avatar asked Mar 12 '10 15:03

coure2011


1 Answers

@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

like image 136
Bakudan Avatar answered Sep 28 '22 05:09

Bakudan