Vishnu
Vishnu

Reputation: 2452

reading json in javascript which is encoded by php

I have json encoded array in php and it looks something like below

["Assam","Chennai","Delhi+%26+NCR","Himachal+Pradesh","Karnataka","Kolkata"];

i wanted to read all the above value in javascript.But i dont get it working.I have read lots of related questions which provide solutions like eval..But i get undefined error.

eval('var result = ' +response);
alert(result.data);

note : var response is returning ["Assam","Chennai","Delhi+%26+NCR","Himachal+Pradesh","Karnataka","Kolkata"]; but result.data is undefined.some one please guide me.

Upvotes: 0

Views: 41

Answers (1)

gen_Eric
gen_Eric

Reputation: 227200

What you have there is an array, not an object. There is no .data property. Try to access it as an array: alert(result[0]);.

Also, please do not use eval() for this purpose. Use JSON.parse().

var result = JSON.parse(response);
alert(response[0]);  // Assam

Upvotes: 2

Related Questions