通过JavaScript迭代/解析JSON对象 [英] Iterating through/Parsing JSON Object via JavaScript

查看:132
本文介绍了通过JavaScript迭代/解析JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了jQuery / Ajax / JSON的问题。我正在使用jQuery ajax这样的帖子......

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

我的理解是这将返回一个JavaScript JSON对象? ajax帖子产生的文本是这个(我相信这是有效的JSON)......

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



<我似乎无法弄清楚如何解析jQuery ajax帖子返回的JSON对象...基本上我想循环并让每个学生返回一个div,就像这样......

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!

推荐答案

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

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"
   }
]

然后你可以迭代它:

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

这篇关于通过JavaScript迭代/解析JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆