我知道我在这里遗漏了一些非常简单的东西,但是我已经搜索了一段时间,而且还没有解决方案的运气。

我有一些像这样的json数据

post[{
//various post data
categories": {

    "Rants": {
        "name": "Rants",
        "slug": "rants",
        "description": "",
        "post_count": 9
}]


我正在尝试使用each()循环返回类别标签

$.each(response.posts[0].categories, function (index) {
                alert(response.posts[0].categories.index.slug);
});


当我调用alert(response.posts[0].categories.rants.slug);时它起作用,但是当我尝试使用索引作为键时却不起作用。

有任何想法吗?

最佳答案

当您编写此代码时:

alert(response.posts[0].categories.index.slug);


您正在尝试读取名为categories"index"属性,该属性不存在。如果您真的想阅读每个元素,则必须编写以下内容:

alert(response.posts[0].categories[index].slug);

07-24 09:38
查看更多