Vuc*_*cko 9 javascript jquery json getjson
这是我从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"
}
}
}]
}]
}
Run Code Online (Sandbox Code Playgroud)
我需要获取最后一个提示文本,编写它的用户以及撰写/发布它的日期.
用户:Damir P.
日期:1314115358
文字:najjači健身中心你毕业
我尝试使用JQuery,这可以获取非数组值:
$.getJSON(url, function(data){
var text= data.response.venue.tips.groups.items.text;
alert(text);
});
Run Code Online (Sandbox Code Playgroud)
但它不适用于数组.
结果:未捕获的TypeError:无法读取未定义的属性"text".
我也试过$ .each,但没有效果.
$.getJSON(url, function(data){
$.each(data.response.venue.tips.groups.items.text, function (index, value) {
console.log(value);
});
});
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么 ?
您需要迭代组和项目.$ .each()将一个集合作为第一个参数,并data.response.venue.tips.groups.items.text 尝试指向一个字符串.这两个groups和items是数组.
详细版本:
$.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'
});
});
});
Run Code Online (Sandbox Code Playgroud)
或者更简单:
$.getJSON(url, function (data) {
$.each(data.response.venue.tips.groups, function (index, value) {
$.each(this.items, function () {
console.log(this.text);
});
});
});
Run Code Online (Sandbox Code Playgroud)
在您指定的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);
});
Run Code Online (Sandbox Code Playgroud)
这会给你:
用户:Damir P.
日期:1314168377
文字:ajd da vidimo hocu li znati ponoviti
你没有循环遍历这些项目.试试这个:
$.getJSON(url, function(data){
$.each(data.response.venue.tips.groups.items, function (index, value) {
console.log(this.text);
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
98746 次 |
| 最近记录: |