0

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.

1
  • 2
    That is an array not an object. There is no .data property. Try alert(result[0]). Also, please use JSON.parse() instead of eval(). Commented Jan 16, 2014 at 16:26

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.