-1

I am trying to access an data inside an Array in Jquery but I am unable to do so. The data is getting received in success function in Ajax. My array looks like this:

[{"id":"22","name":"Ignis","color":"white","manufacturer":"Suzuki","manufacturing_year":"2017"}]

I am trying to access it like:

var obj = data;
var name = obj[0].name;
alert(name);

its getting me undefined but When I store the same received value in store it in variable like below it gives me result:

var mydata = [{"id":"22","name":"Ignis","color":"white","manufacturer":"Suzuki","manufacturing_year":"2017"}];
var obj = mydata;
var name = obj[0].name;
alert(name);

I need every value of the json to be stored in variables named after there respective keys. Please let me know how can I handle this data. Thanks.

6
  • 2
    Why are you unable to do so? What have you tried? Commented Apr 8, 2018 at 19:10
  • Why when you already have simple access using the property keys? This sounds like an XY problem Commented Apr 8, 2018 at 19:11
  • @vikesh you can get a clue from here stackoverflow.com/questions/49606162/… Commented Apr 8, 2018 at 19:13
  • @vikesh could you change your question and write your expected result? Commented Apr 8, 2018 at 19:14
  • What you're trying to do makes absolutely no sense, and there is 100% a better way to do it. Try to create a good question that explains what (code) brought you into the situation. Commented Apr 8, 2018 at 19:14

1 Answer 1

0

I think you're asking for something like this

var  mydata='[{"id":"22","name":"Ignis","color":"white","manufacturer":"Suzuki","manufacturing_year":"2017"} ]';
var mydataobj= JSON.parse(mydata);
var name= mydataobj[0].name;
var id= mydataobj[0].id;
var color=mydata[0].color;

It goes same for all other values

Sign up to request clarification or add additional context in comments.

3 Comments

I have used JSON.parse(data); then tried to access the variable but it still giving me undefined.
Yeah I did that too. It gives me this error: json_decode is not defined
@vikesh I guess this updated answer will solve problem

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.