dulan
dulan

Reputation: 1604

javascript object element bug

I have this javascript object,

console.log(object.response);
console.log(object.response.imageUID);

gives me:

{"id":138,"imageUID":"image-aa0dce87-0261-44ef-8377-d897a996f4b1"}  
undefined  

What went wrong?

Upvotes: 0

Views: 71

Answers (1)

tymeJV
tymeJV

Reputation: 104775

It seemed the issue was that your response was a stringified object - so it probably looked like:

var response = '{"id":138,"imageUID":"image-aa0dce87-0261-44ef-8377-d897a996f4b1"}';

In order to access the properties you must first parse that string into an object:

var responseObj = JSON.parse(response);

Now you can access the properties:

var imageUID = responseObj.imageUID;

Upvotes: 3

Related Questions