167

I have the following JSON returned in a variable called data.

THIS IS THE JSON THAT GETS RETURNED...

[ 
{"Id": 10004, "PageName": "club"}, 
{"Id": 10040, "PageName": "qaz"}, 
{"Id": 10059, "PageName": "jjjjjjj"}
]

and I am trying to loop through the collection using $.each but I am running into problems where the alert is showing undefined. I have tried a lot of different syntax but can't seem to figure this out.

The JQuery I am using is

$.each(data, function(i, item) {
    alert(item.PageName);
});

Can any one point me in the right direction?

EDIT This is the code I am using to grab the data

$.getJSON('/Cms/GetPages/123', null, function(data) {
  fillSelect(data);
});

and this is the function that gets called upon call back

function fillSelect(data) {
  alert(data);
  $.each(data, function(i, item) {
    alert(item.PageName);
  });
}

EDIT 2 This is slightly confusing me, according to the docs it should work as I have it, but it doesn't. According to fiddler the header shows:-

Content-Type: application/json; charset=utf-8

and the JSON is exactly correct above. I am using chrome if this makes any different. Will test in IE and FF....

EDIT 3

using $.get produces

"[\r\n {\r\n \"Id\": 10041,\r\n \"PageName\": \"01234567890\",\r\n \"MetaId\": 1000,\r\n \"TemplateId\": 2\r\n },\r\n {\r\n \"Id\": 10001,\r\n \"PageName\": \"about\",\r\n \"MetaId\": 1000,\r\n \"TemplateId\": 1\r\n },\r\n {\r\n \"Id\": 10056,\r\n \"PageName\": \"fdgdfgdfg\",\r\n \"MetaId\": 1000,\r\n \"TemplateId\": 1\r\n },\r\n {\r\n \"Id\": 10052,\r\n \"PageName\": \"hjkhjk\",\r\n \"MetaId\": 1000,\r\n \"TemplateId\": 2\r\n },\r\n {\r\n \"Id\": 10059,\r\n \"PageName\": \"jjjjjjj\",\r\n \"MetaId\": 1000,\r\n \"TemplateId\": 1\r\n },\r\n {\r\n \"Id\": 10057,\r\n \"PageName\": \"qqqqq\",\r\n \"MetaId\": 1000,\r\n \"TemplateId\": 2\r\n },\r\n {\r\n \"Id\": 10054,\r\n \"PageName\": \"qwqw\",\r\n \"MetaId\": 1000,\r\n \"TemplateId\": 2\r\n }\r\n]"
Cœur
  • 37,241
  • 25
  • 195
  • 267
Rippo
  • 22,117
  • 14
  • 78
  • 117

4 Answers4

329
var data = [ 
 {"Id": 10004, "PageName": "club"}, 
 {"Id": 10040, "PageName": "qaz"}, 
 {"Id": 10059, "PageName": "jjjjjjj"}
];

$.each(data, function(i, item) {
    alert(data[i].PageName);
});

$.each(data, function(i, item) {
    alert(item.PageName);
});

these two options work well, unless you have something like:

var data.result = [ 
 {"Id": 10004, "PageName": "club"}, 
 {"Id": 10040, "PageName": "qaz"}, 
 {"Id": 10059, "PageName": "jjjjjjj"}
];

$.each(data.result, function(i, item) {
    alert(data.result[i].PageName);
});

EDIT:

try with this and describes what the result

$.get('/Cms/GetPages/123', function(data) {
  alert(data);
});

FOR EDIT 3:

this corrects the problem, but not the idea to use "eval", you should see how are the response in '/Cms/GetPages/123'.

$.get('/Cms/GetPages/123', function(data) {
  $.each(eval(data.replace(/[\r\n]/, "")), function(i, item) {
   alert(item.PageName);
  });
});
Sygmoral
  • 7,021
  • 2
  • 23
  • 31
andres descalzo
  • 14,887
  • 13
  • 64
  • 115
  • 1
    Looks like I needed to add `eval(data)` – Rippo Feb 26 '10 at 15:01
  • in the function "httpData" in jQuery you can see a called window["eval"] for the json data. you do not need to use eval. this line "$. get ( '/ Cms/GetPages/123'" is to show you that are receiving in "data". – andres descalzo Feb 26 '10 at 15:40
  • http://james.padolsey.com/jquery/#v=1.3.2&fn=jQuery.ajax http://james.padolsey.com/jquery/#v=1.3.2&fn=jQuery.httpData – andres descalzo Feb 26 '10 at 15:41
  • Have updated answer (see EDIT 3) to show what `$.get` produces – Rippo Feb 26 '10 at 16:02
  • how are you returning the data in "/ Cms/GetPages/123"? – andres descalzo Feb 26 '10 at 16:23
  • Basically its a asp.net mvc project using nhibernate. I am actually using the Newtonsoft.Json dll and overriding the executeResult using the JSonTextWriter and JsonSerializer classes. – Rippo Feb 26 '10 at 16:31
  • `$.each` works for first index not for second, my json object is `{ "0":{"tech_id":"35","tech_name":"Ajax"}, "1":{"tech_id":"36","tech_name":"Ajax"} }` – 151291 Dec 23 '15 at 09:00
17

Have you converted your data from string to JavaScript object?

You can do it with data = eval('(' + string_data + ')'); or, which is safer, data = JSON.parse(string_data); but later will only works in FF 3.5 or if you include json2.js

jQuery since 1.4.1 also have function for that, $.parseJSON().

But actually, $.getJSON() should give you already parsed json object, so you should just check everything thoroughly, there is little mistake buried somewhere, like you might have forgotten to quote something in json, or one of the brackets is missing.

vava
  • 24,851
  • 11
  • 64
  • 79
7
$.each(JSON.parse(result), function(i, item) {
    alert(item.number);
});
António Almeida
  • 9,620
  • 8
  • 59
  • 66
5

getJSON will evaluate the data to JSON for you, as long as the correct content-type is used. Make sure that the server is returning the data as application/json.

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • 1
    according to fiddler the content type is `Content-Type: application/json; charset=utf-8` – Rippo Feb 26 '10 at 15:23