【问题标题】:JQuery get data from JSON arrayJQuery 从 JSON 数组中获取数据
【发布时间】:2013-02-19 14:18:46
【问题描述】:

这是我从foursquare获得的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"
                }
            }
        }]
    }]
}

我需要得到最后一个提示 text ,写它的 用户 以及他写/发布它的 日期

用户:Damir P.

日期:1314115358

文本 em>:Najjači健身中心U gradu

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

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

但它不适用于阵列。

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

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

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

我做错了什么?

【问题讨论】:

  • @jandvorak他在这里学习。没必要烧他。
  • 组也是一个数组。 span>
  • @Johan 我承认,它的表述很糟糕。不过,我只想为他指出正确的资源。

标签: javascript jquery json getjson


【解决方案1】:

您需要迭代组和项目。 $.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 中,最后一个将是:

$.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最后一个是什么? =)
  • 在这种情况下,文本 najjači Fitness centar u gradu 是最后一个。最后一个是 item[0].text 还是 item[1].text ?以及如何用你的代码来解决这个问题?
  • @Vucko 我已经更新了答案。但是您正在谈论的文本是数组中的first,而不是lastlast 是指数组中的最后一个还是 createdAt 日期之前的 latest
  • 我的意思是最后一个创建的。
【解决方案2】:

您没有循环遍历这些项目。试试这个:

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      试试这个

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

      【讨论】:

      • 你好@satish 我们可以通过创建一个数组来使用console.log(this.text);吗?
      猜你喜欢
      • 2019-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多