up vote 0 down vote favorite

Hello, I'm having a problem with jQuery/Ajax/JSON. I'm using a jQuery ajax post like so...

$.ajax({
  type: "POST",
  dataType: "json",
  url: "someurl.com",
  data: "cmd="+escape(me.cmd)+"&q="+q+"&"+me.args,
  success: function(objJSON){
    blah blah...
  }
});

It's my understanding that this will return a JavaScript JSON object? The text that the ajax post produces is this (I believe this is valid JSON)...

{
  "student":{
    "id": 456,
    "full_name": "GOOBER, ANGELA",
    "user_id": "2733245678",
    "stin": "2733212346"
  },
  "student":{
    "id": 123,
    "full_name": "BOB, STEVE",
    "user_id": "abc213",
    "stin": "9040923411"
  }
}

I can't seem to figure out how to parse through the JSON object returned by the jQuery ajax post... basically I want to loop through and make a div out of each student returned like so...

$("<div id=\"" + student.id + "\">" + student.full_name + " (" + student.user_id + " - " + student.stin + ")</div>")

I just can't seem to figure out how to do it...

Please help! Thanks!

link|flag

Can JSON have same key (here "student") i one object multiple times? If not then what you are getting is not JSON. – Kamil Szot Oct 28 '09 at 13:49
1  
no it cannot.... – larson4 Oct 28 '09 at 13:49

3 Answers

up vote 1 down vote accepted

Your JSON object is incorrect because it has multiple properties with the same name. You should be returning an array of "student" objects.

[
   {
     "id": 456,
     "full_name": "GOOBER ANGELA",
     "user_id": "2733245678",
     "stin": "2733212346"
   },
   {
     "id": 123,
     "full_name": "BOB, STEVE",
     "user_id": "abc213",
    "stin": "9040923411"
   }
]

Then you can iterate over it as so:

 for (var i = 0, len = objJSON.length; i < len; ++i) {
     var student = objJSON[i];
     $("<div id=\"" + student.id + "\">" + student.full_name + " (" + student.user_id + " - " + student.stin + ")</div>")...
 }
link|flag
Thanks for the answer, it works! However just a question... how come I can't just do a for (student in objJSON){}? I tried it and it wasn't working... well no matter, it works. Thanks again. – Ryan Oct 28 '09 at 14:32
I would have thought that it would work, but I prefer iterating over arrays using the index. – tvanfosson Oct 28 '09 at 14:42
@tvanfosson stackoverflow.com/questions/2498107/…... – Pandiya Chendur Mar 23 at 7:01
up vote 2 down vote

The JSON you have currently will not work because the second "student" effectively replaces the first, since these are just properties in an object.

If you can control the JSON output, change it to:

{
  "student":[{
    "id": 456,
    "full_name": "GOOBER, ANGELA",
    "user_id": "2733245678",
    "stin": "2733212346"
    },{
    "id": 123,
    "full_name": "BOB, STEVE",
    "user_id": "abc213",
    "stin": "9040923411"
  }]
}

Then you have an an array which you can loop over:

for(var i=0; i<objJSON.student.length; i++) {
  ...objJSON.student[i]...
  }
link|flag
up vote 1 down vote

There's a problem with that JSON - the outer curly braces should be square brackets, as you want a list, not a map (aka object). With all objects in the map having the same key, it won't parse right - the last value will overwrite the previous under most parsers.

You want:

[
  {
    "id": 456,
    "full_name": "GOOBER, ANGELA",
    "user_id": "2733245678",
    "stin": "2733212346"
  },
  {
    "id": 123,
    "full_name": "BOB, STEVE",
    "user_id": "abc213",
    "stin": "9040923411"
  }
]

or alternatively a map with different keys:

{
  "456":{
    "id": 456,
    "full_name": "GOOBER, ANGELA",
    "user_id": "2733245678",
    "stin": "2733212346"
  },
  "123":{
    "id": 123,
    "full_name": "BOB, STEVE",
    "user_id": "abc213",
    "stin": "9040923411"
  }
}

You'd be better off using $.getJSON, which is a specialised version of $.ajax for this purpose.

$.getJSON("someurl.com", function(students) {
  $.each(students, function() { 
    $("<div id=\"" + student.id + "\">" + student.full_name + " (" + student.user_id + " - " + student.stin + ")</div>").appendTo($container);
  });
});
link|flag
There's an error in your first suggestion: you still have property names ("student") for entries in your array. Will remove downvote when fixed. – Tim Down Oct 28 '09 at 14:26
that's fixed, thanks. – mahemoff Oct 29 '09 at 14:55

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.