0

如何遍历这个 json 对象来获取每个项目的值?我知道这很容易,但我需要不明白为什么这个 json 对象的第一个和结尾有两个括号 ([])。

[// I'm talking about this 
    [
        {
            "id": 2,
            "title": "xxxxxxxxx",
            "author": "mike123",
            "postdate": "March 12, 2013 at 6:46 pm",
            "postdatecreation": "2013-03-12",
            "posteditdate": null,
            "postcontent": "eeeeee",
            "userID": 34
        }
    ]
]// and this 

如果我删除它们,json 仍然有效。

4

1 回答 1

0

您可以使用 $.each 循环遍历您的 json 对象。这是小提琴:

http://jsfiddle.net/Ay2UB/

这里的结果是一个对象数组的数组。我通过访问索引传递了该对象 results[0] 会给你一个数组 results[0][0] 会给你对象

下面的代码:

var results=[
    [
        {
            "id": 2,
            "title": "xxxxxxxxx",
            "author": "mike123",
            "postdate": "March 12, 2013 at 6:46 pm",
            "postdatecreation": "2013-03-12",
            "posteditdate": null,
            "postcontent": "eeeeee",
            "userID": 34
        }
    ]
]

$.each(results[0][0],function(key, value){
  alert(value);
});

$.each 可用于遍历数组或对象,请按照以下链接获取更多信息:

http://api.jquery.com/jQuery.each/

希望能帮助到你。如果我错了,请纠正我。

于 2013-03-25T03:45:47.010 回答