jQuery jQuery解析JSON多维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2487841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:36:56  来源:igfitidea点击:

jQuery parse JSON multidimensional array

jqueryjsonarrays

提问by ChrisMJ

I have a JSON array like this:

我有一个像这样的 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"
    }
  ]
}

I want to use jQuery .getJSONto 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.getJSON来返回每个数组值的值,但我不确定如何访问它们。

So far I have this jQuery code:

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

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

        });

How can I do this with jQuery?

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

回答by BalusC

The {}in JSON represents an object. Each of the object's properties is represented by key:valueand 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 inloop:

{}在JSON表示一个对象。对象的每个属性都用key:value逗号分隔来表示。属性值可以通过使用句点运算符的键访问,就像这样json.forum。的[]在JSON表示一个数组。数组值可以是任何对象,并且值以逗号分隔。要遍历数组,请使用带有索引的标准 for 循环。要迭代对象的属性而不直接通过键引用它们,您可以使用for in循环:

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

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

如果你想以 jQueryish 的方式做到这一点,请抓住$.each()

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

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.

我使用了与“普通 JavaScript”方式相同的变量名,以便您更好地理解 jQuery 用它“在幕后”做了什么。希望这可以帮助。

回答by Samir Talwar

That should work fine. Just use $.getJSONinstead of $.get.

那应该可以正常工作。只需使用$.getJSON代替$.get.