-2

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

Md Nazmul Hossain
  • 2,768
  • 5
  • 26
  • 49
  • is this a server response or what? anyway, `response[0].body.token` will gove you token. – marmeladze Jul 28 '16 at 10:57
  • JSON is a *textual notation* for data exchange. [(More)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. Moreover, if that *were* meant to be JSON, it would be invalid (keys and strings must be in double quotes). – T.J. Crowder Jul 28 '16 at 10:58

3 Answers3

0

var myObj = [
   {
      body:
        {
           token : 'xxxxxxx'
        }
   },
   {
      other : 'aaa'
   }

];
console.log(myObj[0].body.token);
//or other version
var bodyObj = myObj.find(function(item){ return item && item.body});
console.log(bodyObj.body.token);
Vladu Ionut
  • 8,075
  • 1
  • 19
  • 30
0

Assuming you have parsed the JSON (and that it's valid JSON, which it isn't in the question) and assigned the result to data, you access it like this:

data[0].body.token

...because:

  • data refers to an array
  • [0] accesses the first entry in the array, which refers to an object
  • .body accesses that object's body property value, which refers to another object
  • .token accesses that object's token property value

E.g.:

var json = '[{"body":{"token":"xxxxxxx"}},{"other":"aaa"}]';
var data = JSON.parse(json)
console.log(data[0].body.token);
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

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
Zahirul Haque
  • 1,599
  • 17
  • 22