Deepak Bandi
Deepak Bandi

Reputation: 1904

Get data from the response

I have a response which is of the below format,

{
  "access_token": "eWcWLctGW-_NgGVAmFbO9l-nt3yztFzlZCLLlilI9mGDcM5q8d0kQw0uzvFOoXynHcb-MuPVJGTGkSkBhrr69_-aN1r5j9zB4fCl4u4aqOQ-scNI36xgHeGYpXky60drIBpMI83FGqd9pMjL4GWXjFHq61nhJ6xkGj1u1r9a5u6EJrB1lfjNhljzC_j65xaqxtubQ4AglKFO2ib-levpvnd_bEU-QGQrtvS2QbaXhb_hlnX8czo61Gn_OQyBVk7HbN1SozxIPe3RBvf5AiCAouDMz1WMHy9ybVFy8SnoNIgszjo7Ev2IEWS9aFb87u6bvoJvSVJv7s3z-2GUvG2kwfOk2sUWrmq0QeIJJrYwdKQfs3T8HrK2MNKSGteJ04-O",
  "token_type": "bearer",
  "expires_in": 1799,
  "refresh_token": "f1005c7fd74247069dbdb078ee379410",
  "as:client_id": "438dc832-33c7-413b-9c71-d0b98a196e6a",
  "userName": "master",
  ".issued": "Fri, 20 Jan 2017 14:30:09 GMT",
  ".expires": "Fri, 20 Jan 2017 15:00:09 GMT"
}

I'm not sure how to access .issued , .expires and as:client_id

I'm using angular and passing username, password and company_id and getting the response in the above format.

dataService.getAuthToken($scope.username, $scope.password, $scope.company_password).then(function (response) {
  
    //response data here

});

I can easily get token_type, access_token by just using response.data.access_token but not sure how to access .issued , .expires and as:client_id

Upvotes: 2

Views: 257

Answers (1)

Cyril Gandon
Cyril Gandon

Reputation: 17048

You can access every property of an object in JavaScript by the indexer syntax, like if it was a map (because an object is a map in javascript):

var issued = response.data[".issued"];
var expires = response.data[".expires"];
var asClient_id= response.data["as:client_id"];

See this link: http://www.w3schools.com/js/js_objects.asp

Accessing Object Properties
You can access object properties in two ways:

objectName.propertyName
or
objectName["propertyName"]

Upvotes: 5

Related Questions