首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JQuery从JSON数组获取数据

JQuery从JSON数组获取数据
EN

Stack Overflow用户
提问于 2013-03-05 08:51:20
回答 4查看 114.3K关注 0票数 9

这是我从foursquare获得的JSON的一部分。

JSON

代码语言:javascript
运行
复制
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"
                }
            }
        }]
    }]
}

我需要得到text的最后一个提示,编写它的用户和他写/发布它时的date

用户: Damir P.

日期: 1314115358

文字: najjači健身中心u级

我尝试使用JQuery来获取一个非数组值:

代码语言:javascript
运行
复制
$.getJSON(url, function(data){
    var text= data.response.venue.tips.groups.items.text;
    alert(text);
});

但它不适用于数组。

结果:Uncaught :无法读取未定义的属性“text”。

我也试过使用$.each,但没有效果。

代码语言:javascript
运行
复制
$.getJSON(url, function(data){
    $.each(data.response.venue.tips.groups.items.text, function (index, value) {
        console.log(value);
    });
});

我做错什么了?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-03-05 09:10:37

您需要同时迭代组和项。$.each()将集合作为第一个参数,data.response.venue.tips.groups.items.text试图指向字符串。groupsitems都是数组。

详细版本:

代码语言:javascript
运行
复制
$.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'
        });
    });
});

或者更简单地说:

代码语言:javascript
运行
复制
$.getJSON(url, function (data) {
    $.each(data.response.venue.tips.groups, function (index, value) {
        $.each(this.items, function () {
            console.log(this.text);
        });
    });
});

在您指定的JSON中,最后一个应该是:

代码语言:javascript
运行
复制
$.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

票数 10
EN

Stack Overflow用户

发布于 2013-03-05 08:54:19

你不能在这些东西上乱翻。试一试:

代码语言:javascript
运行
复制
$.getJSON(url, function(data){
    $.each(data.response.venue.tips.groups.items, function (index, value) {
        console.log(this.text);
    });
});
票数 6
EN

Stack Overflow用户

发布于 2013-03-05 08:56:03

我想你需要这样的东西:

代码语言:javascript
运行
复制
var text= data.response.venue.tips.groups[0].items[1].text;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15219435

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档