通过JavaScript迭代/解析JSON对象

10

我在使用jQuery/Ajax/JSON时遇到了问题。类似于以下方式,我正在使用jQuery ajax post...

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

我的理解是这将返回一个JavaScript JSON对象?ajax post生成的文本如下(我认为这是有效的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"
  }
}

我似乎无法弄清楚如何解析由jQuery ajax post返回的JSON对象...基本上,我想循环遍历每个返回的学生,并制作一个div,就像这样...

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

我似乎无法弄清楚如何做到这一点...

谢谢!


JSON对象中的同一对象可以多次拥有相同的键(例如“student”)吗?如果不行,那么你得到的就不是JSON。 - Kamil Szot
啊,是的,“ANGELA GOOBER”……最近小安吉怎么样了?她和“BOBBY DOOFUS”最后有没有结婚呢? - ashleedawg
4个回答

32

你的 JSON 对象不正确,因为它有多个具有相同名称的属性。你应该返回一个“student”对象数组。

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

那么你可以这样迭代它:

 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>")...
 }

2
谢谢你的回答,它起作用了!不过只是一个问题...为什么我不能只做一个for (student in objJSON)?我试过了,但它没起作用...无论如何,它现在能运行了。再次感谢。 - Ryan
我本以为它会起作用,但我更喜欢使用索引迭代数组。 - tvanfosson
迭代JSON对象似乎对我无效... - ACP
这里有一个问题。我相信 JSON 列表中的每个对象都是一个字典。如果我们在普通的 Python 中执行 some_dictionary.attribute,会得到一个 AttributeError。那么在这里它是有效的吗?它是一种不同类型的字典,还是根本不像字典? - Sidharth Samant
@SidharthSamant 这是 JavaScript。在 JavaScript 中,只要属性名称符合 JavaScript 命名规范,你可以使用 obj['property']obj.property 来引用对象的属性。如果属性名称包含 JavaScript 名称中不允许的字符,则只能使用第一种选项。 - tvanfosson
@tvanfosson 对啊,我忘了。抱歉。我在谷歌上搜索了几个小时我的Python问题,当我偶然发现这里时,我甚至没有看到这个问题是否被标记为Python。我的错。 - Sidharth Samant

4

您当前拥有的JSON无法使用,因为第二个“student”实际上替换了第一个,“student”只是对象中的属性。

如果您可以控制JSON输出,请将其更改为:

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

然后你会得到一个数组,你可以对其进行循环:
for(var i=0; i<objJSON.student.length; i++) {
  ...objJSON.student[i]...
  }

4

这个JSON有问题——外层大括号应该改成中括号,因为你想要一个列表而不是映射(又称对象)。在映射中所有对象都有相同的键时,它无法正确解析。在大多数解析器下,最后一个值将覆盖以前的值。

你需要:

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

或者使用具有不同键的映射:
{
  "456":{
    "id": 456,
    "full_name": "GOOBER, ANGELA",
    "user_id": "2733245678",
    "stin": "2733212346"
  },
  "123":{
    "id": 123,
    "full_name": "BOB, STEVE",
    "user_id": "abc213",
    "stin": "9040923411"
  }
}

你最好使用$.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);
  });
});

你的第一个建议中有错误:你在数组条目中仍然有属性名称(“student”)。当修复后将取消踩。 - Tim Down

1
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);    
})

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接