我遇到了jQuery/Ajax/JSON的问题.我正在使用jQuery ajax这样的帖子......
$.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帖子产生的文本就是这个(我相信这是有效的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帖子返回的JSON对象...基本上我想循环并让每个学生返回一个div像这样......
$("" + student.full_name + " (" + student.user_id + " - " + student.stin + ")")
我似乎无法弄清楚如何做到这一点......
谢谢!
您的JSON对象不正确,因为它具有多个具有相同名称的属性.你应该返回一组"学生"对象.
[ { "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]; $("" + student.full_name + " (" + student.user_id + " - " + student.stin + ")")... }