2013-03-05 175 views
7

这是我从foursquare得到的JSON的一部分。JQuery从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" 
       } 
      } 
     }] 
    }] 
} 

我需要得到最后一个技巧文本,则用户谁写的和日期时,他写道/张贴。

用户:达米尔P.

日期:1314115358

文本:najjači健身辛塔尔ü渐亮

我试着用JQuery的和这个作品来获取非 - 阵列值:

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

但它不适用于数组。

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

此外我尝试与$。每个,但没有效果。

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

我在做什么错了?

+0

@JanDvorak他在这里学习。无需火焰他。 – Johan 2013-03-05 08:56:30

+0

组也是一个数组。 – SteveP 2013-03-05 08:56:39

+0

@Johan我承认,这是非常制定。尽管如此,我想要的只是将他指向正确的资源。 – 2013-03-05 08:58:36

回答

8

您需要遍历两个组和项目。 $.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); 
}); 

这将使你:

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

+0

非常好。这获得所有的数据,但如何获得最后一个? – Vucko 2013-03-05 09:20:08

+0

@Vucko什么是*最后一个*? =) – 2013-03-05 09:23:25

+1

在这种情况下,文本**najjači健身centar u毕业**是最后一个。那么最后一个'item [0] .text'或'item [1] .text'?以及如何用你的代码来解决这个问题? – Vucko 2013-03-05 09:27:50

6

你没有在项目上循环。试试这个:

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

我想你需要这样的东西:

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

试试这个

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

hello @satish我们可以使用console.log(this.text);'通过创建数组来创建每个数组? – SagarPPanchal 2014-04-21 12:43:49