jQuery的解析JSON多维数组 [英] jQuery parse JSON multidimensional array

查看:211
本文介绍了jQuery的解析JSON多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON数组是这样的:

I have a JSON array like this:

{
  "forum":[
    {
      "id":"1",
      "created":"2010-03-19 ",
      "updated":"2010-03-19 ","user_id":"1",
      "vanity":"gamers",
      "displayname":"gamers",
      "private":"0",
      "description":"All things gaming",
      "count_followers":"62",
      "count_members":"0",
      "count_messages":"5",
      "count_badges":"0",
      "top_badges":"",
      "category_id":"5",
      "logo":"gamers.jpeg",
      "theme_id":"1"
    }
  ]
}

我想使用jQuery .getJSON 来能够返回各自为阵的值的值,但我不知道是如何可以访问它们。

I want to use jQuery .getJSON to be able to return the values of each of the array values, but I'm not sure as to how to get access to them.

到目前为止,我有这个jQuery code:

So far I have this jQuery code:

$.get('forums.php', function(json, textStatus) {
            //optional stuff to do after success
            alert(textStatus);
            alert(json);

        });

我怎样才能做到这一点使用jQuery?

How can I do this with jQuery?

推荐答案

{} 在JSON重新presents的对象。每个对象的属性是重新通过 psented $ P $键:值和逗号分隔。该属性值是通过使用像这样 json.forum 期间运营商的关键访问。在 [] 在JSON重新presents阵列。数组值可以是任何对象和值逗号分隔。遍历数组,使用循环标准与指标。遍历对象的属性不受你可以在使用重点直接引用他们循环:

The {} in JSON represents an object. Each of the object's properties is represented by key:value and comma separated. The property values are accessible by the key using the period operator like so json.forum. The [] in JSON represents an array. The array values can be any object and the values are comma separated. To iterate over an array, use a standard for loop with an index. To iterate over object's properties without referencing them directly by key you could use for in loop:

var json = {"forum":[{"id":"1","created":"2010-03-19 ","updated":"2010-03-19 ","user_id":"1","vanity":"gamers","displayname":"gamers","private":"0","description":"All things gaming","count_followers":"62","count_members":"0","count_messages":"5","count_badges":"0","top_badges":"","category_id":"5","logo":"gamers.jpeg","theme_id":"1"}]};

var forum = json.forum;

for (var i = 0; i < forum.length; i++) {
    var object = forum[i];
    for (property in object) {
        var value = object[property];
        alert(property + "=" + value); // This alerts "id=1", "created=2010-03-19", etc..
    }
}

如果你想这样做的jQueryish方式,抢 $。每个()

If you want to do this the jQueryish way, grab $.each():

$.each(json.forum, function(i, object) {
    $.each(object, function(property, value) {
        alert(property + "=" + value);
    });
});

我已经使用了相同的变量名称为普通的JavaScript的方式,让你更好地理解的jQuery做什么下罩的程度。希望这有助于。

I've used the same variable names as the "plain JavaScript" way so that you'll understand better what jQuery does "under the hoods" with it. Hope this helps.

这篇关于jQuery的解析JSON多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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