jQuery从JSON数组获取数据

9
这是我从Foursquare获取的JSON部分。
JSON(JavaScript Object Notation)
tips: {
    count: 2,
    groups: [
    {
        type: "others",
        name: "Tips from others",
        count: 2,
        items: [
        {
            id: "4e53cf1e7d8b8e9188e20f00",
            createdAt: 1314115358,
            text: "najjači fitness centar u gradu",
            canonicalUrl: "https://foursquare.com/item/4e53cf1e7d8b8e9188e20f00",
            likes: {
                count: 2,
                groups: [
                {
                    type: "others",
                    count: 2,
                    items: []
                }],
                summary: "2 likes"
            },
            like: false,
            logView: true,
            todo: {
                count: 0
            },
            user: {
                id: "12855147",
                firstName: "Damir",
                lastName: "P.",
                gender: "male",
                photo: {
                    prefix: "https://irs1.4sqi.net/img/user/",
                    suffix: "/AYJWDN42LMGGD2QE.jpg"
                }
            }
        },
        {
            id: "4e549e39152098912f227203",
            createdAt: 1314168377,
            text: "ajd da vidimo hocu li znati ponoviti",
            canonicalUrl: "https://foursquare.com/item/4e549e39152098912f227203",
            likes: {
                count: 0,
                groups: []
            },
            like: false,
            logView: true,
            todo: {
                count: 0
            },
            user: {
                id: "12855147",
                firstName: "Damir",
                lastName: "P.",
                gender: "male",
                photo: {
                    prefix: "https://irs1.4sqi.net/img/user/",
                    suffix: "/AYJWDN42LMGGD2QE.jpg"
                }
            }
        }]
    }]
}

我需要获取最后一个提示文本,以及撰写它的用户和他/她发表时的日期。
用户:Damir P.
日期:1314115358
文本:城市中最棒的健身中心
我尝试使用JQuery,并且这可以获取非数组值。
$.getJSON(url, function(data){
    var text= data.response.venue.tips.groups.items.text;
    alert(text);
});

但是它不能处理数组。

结果:未捕获的类型错误:无法读取未定义的属性“text”。

我还尝试使用 $.each ,但没有效果。

$.getJSON(url, function(data){
    $.each(data.response.venue.tips.groups.items.text, function (index, value) {
        console.log(value);
    });
});

我做错了什么吗?

@JanDvorak 他来这里是为了学习,没有必要对他进行攻击。 - Johan
groups也是一个数组。 - SteveP
@Johan 我承认,那句话表达得很糟糕。但我只是想指引他去正确的资源。 - John Dvorak
4个回答

10
你需要迭代组和项。$.each()将集合作为第一个参数,并且data.response.venue.tips.groups.items.text尝试指向一个字符串。 groupsitems都是数组。
$.getJSON(url, function (data) {

    // Iterate the groups first.
    $.each(data.response.venue.tips.groups, function (index, value) {

        // Get the items
        var items = this.items; // Here 'this' points to a 'group' in 'groups'

        // Iterate through items.
        $.each(items, function () {
            console.log(this.text); // Here 'this' points to an 'item' in 'items'
        });
    });
});

或更简单地说:
$.getJSON(url, function (data) {
    $.each(data.response.venue.tips.groups, function (index, value) {
        $.each(this.items, function () {
            console.log(this.text);
        });
    });
});

在您指定的JSON中,last 的值将是:
$.getJSON(url, function (data) {
    // Get the 'items' from the first group.
    var items = data.response.venue.tips.groups[0].items;

    // Find the last index and the last item.
    var lastIndex = items.length - 1;
    var lastItem = items[lastIndex];

    console.log("User: " + lastItem.user.firstName + " " + lastItem.user.lastName);
    console.log("Date: " + lastItem.createdAt);
    console.log("Text: " + lastItem.text);
});

这将为您提供:

用户:Damir P.
日期:1314168377
文本:ajd da vidimo hocu li znati ponoviti


很好。这可以获取所有数据,但如何获取最后一个? - Vucko
@Vucko 最后一个是什么?=) - Mario S
1
在这种情况下,“najjači fitness centar u gradu”是最后一个。那么最后一个是item[0].text还是item[1].text?你如何使用你的代码来解决这个问题? - Vucko
@Vucko 我已经更新了答案。但是你所说的文本是数组中的第一个,而不是最后一个。你所说的最后一个,是指数组中的最后一个还是按createdAt日期排序的最新的? - Mario S
我指的是最后创建的那个。 - Vucko
@Vucko 好的,那么在获取项目之前,只需对items数组中的createdAt进行排序即可。请参考 此处 了解如何对数组进行排序。我相信您一定能搞定 =) - Mario S

6
您没有对项目进行循环遍历。请尝试使用以下代码代替:
```python 您的代码 ```
注意:请勿删除HTML标签。
$.getJSON(url, function(data){
    $.each(data.response.venue.tips.groups.items, function (index, value) {
        console.log(this.text);
    });
});

0

试一下这个

$.getJSON(url, function(data){
    $.each(data.response.venue.tips.groups.items, function (index, value) {
        console.log(this.text);
    });
});

你好 @satish,我们可以通过创建数组来使用 console.log(this.text); 来输出每个吗? - SagarPPanchal

0

我认为你需要类似这样的东西:

var text= data.response.venue.tips.groups[0].items[1].text;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接