Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print json data.

I have a json output array like this

{
   "data": [
      {
         "name": "Ben Thorpe",
         "id": "XXXXXXXXXXX"
      },
      {
         "name": "Francis David",
         "id": "XXXXXXXXXXX"
      },
}

I want to loop through it and print out the all the names using javascript. I want to be able to do this.

for(i=0;i<length;i++){
      var result += response.data[i].name + ', ';
}

But I am unable to find the length of the json object using javascript.

like image 503
Nick Avatar asked Jul 24 '10 20:07

Nick


1 Answers

response.data is an array of objects, thus has a length property that you can use to iterate its elements.

var result;

for(var i=0;i<response.data.length;i++)
{
      result += response.data[i].name  + ', ';

}
like image 82
Sky Sanders Avatar answered Nov 04 '22 07:11

Sky Sanders