JQuery从JSON数组中获取数据

编程入门 行业动态 更新时间:2024-10-27 06:20:27
本文介绍了JQuery从JSON数组中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

This is part of the JSON i get from foursquare.

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: "foursquare/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: "irs1.4sqi/img/user/", suffix: "/AYJWDN42LMGGD2QE.jpg" } } }, { id: "4e549e39152098912f227203", createdAt: 1314168377, text: "ajd da vidimo hocu li znati ponoviti", canonicalUrl: "foursquare/item/4e549e39152098912f227203", likes: { count: 0, groups: [] }, like: false, logView: true, todo: { count: 0 }, user: { id: "12855147", firstName: "Damir", lastName: "P.", gender: "male", photo: { prefix: "irs1.4sqi/img/user/", suffix: "/AYJWDN42LMGGD2QE.jpg" } } }] }] }

我需要得到最后一个提示文字,编写它的用户以及撰写/发布时的日期。

I need to get the last tip text , the user who wrote it and the date when he wrote/post it.

用户:Damir P。

日期 :1314115358

Date : 1314115358

文字:najjač健身中心你毕业

Text: najjači fitness centar u gradu

我试过 JQuery ,这可以获取非数组值:

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

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

但它不适用于数组。

结果:未捕获的TypeError:无法读取未定义的属性text。

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

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

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

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

我做错了什么?

推荐答案

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

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

详细版本:

$.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

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

更多推荐

JQuery从JSON数组中获取数据

本文发布于:2023-10-12 02:54:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483545.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中   数据   JQuery   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!