JQuery 从 JSON 数组中获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15219435/
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 14:36:54  来源:igfitidea点击:

JQuery get data from JSON array

javascriptjqueryjsongetjson

提问by Vucko

This is part of the JSON i get from foursquare.

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

JSON

JSON

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"
                }
            }
        }]
    }]
}

I need to get the last tip text, the userwho wrote it and the datewhen he wrote/post it.

我需要获取最后一个提示文本、编写它的用户以及他编写/发布它的日期

User: Damir P.

用户: Damir P.

Date: 1314115358

日期: 1314115358

Text: najja?i fitness centar u gradu

文字:najja?i Fitness centar u gradu

I tried with JQueryand this works to fetch a non-array value:

我尝试使用JQuery,这可以获取非数组值:

$.getJSON(url, function(data){
    var text= data.response.venue.tips.groups.items.text;
    alert(text);
});

But it doesn't work with arrays.

但它不适用于数组。

Result: Uncaught TypeError: Cannot read property 'text' of undefined.

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

Also I tried with $.each, but with no effect.

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

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

What am I doing wrong ?

我究竟做错了什么 ?

采纳答案by Mario Sannum

You need to iterate both the groups and the items. $.each()takes a collection as first parameter and data.response.venue.tips.groups.items.texttriesto point to a string. Both groupsand itemsare arrays.

您需要迭代组和项目。$.each()将集合作为第一个参数并data.response.venue.tips.groups.items.text尝试指向一个字符串。这两个groupsitems是数组。

Verbose version:

详细版本:

$.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'
        });
    });
});

Or more simply:

或者更简单:

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


In the JSON you specified, the lastone would be:

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

$.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);
});

This would give you:

这会给你:

User: Damir P.
Date: 1314168377
Text: ajd da vidimo hocu li znati ponoviti

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

回答by sroes

You're not looping over the items. Try this instead:

你没有遍历项目。试试这个:

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

回答by SteveP

I think you need something like:

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

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

回答by Satish Sharma

try this

尝试这个

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