0

I have an ajax fn. My data variable gives me this response

[{"id":1,"name":"x"},{"id":2,"name":"y"},{"id":3,"name":"z"}]

How do I get id,name. I tried using

JSON.Parse(data[i])
    for(var i=0; i <data.length; i++){
      console.log(data[i].id);
    }

My console says unexpected end of data.

2
  • 1
    first of all the method is JSON.parse second, make sure you get the proper response - is your data the string or is it actually already parsed object (which is default for jquery if server responds with the proper content-type)? Commented Apr 13, 2013 at 9:17
  • @Guard:In this link datamapper.wanwizard.eu/pages/extensions/array.html. I am using all_to_array(); Commented Apr 13, 2013 at 9:24

2 Answers 2

1

use this.it may help

data=$.parseJSON(data)
$.each(data,function(i,e){
    alert(e.id);
});
Sign up to request clarification or add additional context in comments.

4 Comments

:TypeError: $.ParseJSON is not a function [Break On This Error] data=$.ParseJSON(data)
@user2261231 try with lowercase p
@JanDvorak:It is alreting.I do not want to alert.What is e here ?
@user2261231 the alert is just to demonstrate how to access the ID. e is the array element whose ID you want.
1
    data = JSON.parse(data)
    for(var i=0; i <data.length; i++){
      alert(data[i].id);
      alert(data[i].name);
    }

2 Comments

that is incorrect JSON.Parse(data[i]) it should be JSON.Parse(data)
@MohammadAdil actually, it should be JSON.parse(data)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.