Md. Nazmul Hossain Bilash Md. Nazmul Hossain Bilash - 4 months ago 6
JSON Question

How can I access json data inside array?

I need some help for a json value. I data is -

[
{
"statusCode":200,
"body":{
"token":"xxxxx"
},
"headers":{
"date":"Thu, 28 Jul 2016 11:03:17 GMT",
"server":"Apache/2.2.15 (CentOS)",
"x-powered-by":"PHP/5.6.22",
"cache-control":"private, must-revalidate",
"etag":"\"9517ef72d528ad7a3bc04c64d1cc1cc9\"",
"set-cookie":[
"XSRF-TOKEN=xxx; expires=Thu, 28-Jul-2016 13:03:17 GMT; Max-Age=7200; path=/",
"laravel_session=eyJpdiI6IkZITXdyTGtpZlRkc1hmQkptUWpZSEE9PSIsInZhbHVlIjoicGxLUmJxRzlcL2dGTTdVcVJiQ1g2QTh4enQxdDI5NElCbGJkVllKYVR0MG1LQTljaFhhUFJSUVVXTytheUxqajZjV3FVUkh2SUhPK0ZtelhIQjcxVk5nPT0iLCJtYWMiOiJkOTg1MWFiYjY5ZTdhNThkODk5N2Y1MmRlOWEwZWMwYWQ4MGE4ZDVjMWRjMGMwNjA0MTlmNjQ1YzNmNDM3NWVkIn0%3D; expires=Thu, 28-Jul-2016 13:03:17 GMT; Max-Age=7200; path=/; httponly"
],
"vary":"Accept-Encoding",
"content-length":"305",
"connection":"close",
"content-type":"application/json"
},
"request":{
"uri":{
"protocol":"http:",
"slashes":true,
"auth":null,
"host":"api.shobkichhu.com",
"port":80,
"hostname":"api.shobkichhu.com",
"hash":null,
"search":null,
"query":null,
"pathname":"/api/auth/login",
"path":"/api/auth/login",
"href":"http://api.shobkichhu.com/api/auth/login"
},
"method":"POST",
"headers":{
"content-type":"application/json",
"accept":"application/json",
"content-length":45
}
}
},
{
"token":"xxxx"
}
]


I need to access token. How can I access token ?

Thanks in advance

Answer

This is the solution

var data =[  
   {  
      "statusCode":200,
      "body":{  
         "token":"xxxxx"
      },


   },
   {  
      "token":"xxxx"
   }
];


console.log(data[0].body.token); // for first token
console.log(data[1].token); // for second token