简体   繁体   English

通过JavaScript迭代/解析JSON对象

[英]Iterating through/Parsing JSON Object via JavaScript

I'm having a problem with jQuery/Ajax/JSON. 我遇到了jQuery / Ajax / JSON的问题。 I'm using a jQuery ajax post like so... 我正在使用jQuery ajax这样的帖子......

$.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? 我的理解是这将返回一个JavaScript JSON对象? The text that the ajax post produces is this (I believe this is valid JSON)... ajax帖子产生的文本就是这个(我相信这是有效的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... 我似乎无法弄清楚如何解析jQuery ajax帖子返回的JSON对象...基本上我想循环并让每个学生返回一个div,就像这样......

$("<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... 我似乎无法弄清楚如何做到这一点......

Thanks! 谢谢!

Your JSON object is incorrect because it has multiple properties with the same name. 您的JSON对象不正确,因为它具有多个具有相同名称的属性。 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>")...
 }

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). JSON有一个问题 - 外部花括号应该是方括号,因为你想要一个列表,而不是一个地图(又称对象)。 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,这是$ .ajax的专用版本。

$.getJSON("someurl.com", function(students) {
  $.each(students, function() { 
    $("<div id=\"" + student.id + "\">" + student.full_name + " (" + student.user_id + " - " + student.stin + ")</div>").appendTo($container);
  });
});

The JSON you have currently will not work because the second "student" effectively replaces the first, since these are just properties in an object. 您当前的JSON将无法工作,因为第二个“学生”有效地替换了第一个,因为这些只是对象中的属性。

If you can control the JSON output, change it to: 如果您可以控制JSON输出,请将其更改为:

{
  "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]...
  }
var data = '[
  {
      "id": 456,
      "full_name": "GOOBER, ANGELA",
      "user_id": "2733245678",
      "stin": "2733212346"
  },
  {
      "id": 123,
      "full_name": "BOB, STEVE",
      "user_id": "abc213",
      "stin": "9040923411"
   }
]';

$.each(data, function(index, val) {
    alert(val.id);
    alert(val.full_name);
    alert(val.user_id);
    alert(val.stin);    
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM