gpt4 book ai didi

jquery - 使用 jQuery 解析 JSON 文件?

转载 作者:行者123 更新时间:2023-12-01 05:01:15 24 4
gpt4 key购买 nike

我有一个函数访问 JSON 文件,我试图从 JSON 文件中获取照片的 URL,但我似乎无法深入到文件中来获取它们?

这是函数:

var pics = [];

function jsonData(){
$.ajax({
url: "https://api.foursquare.com/v2/users/self/checkins?oauth_token=FUKXDJRWIB0AQ2MQUKUEUSB3KW2TMYKUMFGYLYUHBBH14CQ0&v=20120126",
cache: false,
dataType: 'json',
success: function(results) {
var lat;
var long;
var paths = [];
for(var i = 0; i < results.response.checkins.items.length; i++) {
var pic = results.response.checkins.items[i].photos.items[0].sizes.items[0];
pics.push(pic);
}
}
});

};

这是 JSON 的样子,或者我关注的部分,我正在尝试获取 photos.items[1],它们是 WxH = 300 的照片:

{
"meta": {
"code": 200
},
"notifications": [
{
"type": "notificationTray",
"item": {}
}
],
"response": {
"checkins": {
"count": 1385,
"items": [
{
"id": "4f71b513e4b0684643f7929e",
"createdAt": 1332851987,
"type": "checkin",
"shout": "Fish are still alive",
"timeZone": "America/New_York",
"timeZoneOffset": -240,
"venue": {},
"photos": {
"count": 1,
"items": [
{
"id": "4f71b515e4b0559c393d50dc",
"createdAt": 1332851989,
"url": "https://is1.4sqi.net/pix/t3wej3CK8o3DbJrpOZKYtO6ps1UNMZ05jr0T_BxPBX0.jpg",
"sizes": {
"count": 4,
"items": [
{
"url": "https://is1.4sqi.net/pix/t3wej3CK8o3DbJrpOZKYtO6ps1UNMZ05jr0T_BxPBX0.jpg",
"width": 720,
"height": 537
},
{
"url": "https://is0.4sqi.net/derived_pix/t3wej3CK8o3DbJrpOZKYtO6ps1UNMZ05jr0T_BxPBX0_300x300.jpg",
"width": 300,
"height": 300
},
{
"url": "https://is0.4sqi.net/derived_pix/t3wej3CK8o3DbJrpOZKYtO6ps1UNMZ05jr0T_BxPBX0_100x100.jpg",
"width": 100,
"height": 100
},
{
"url": "https://is0.4sqi.net/derived_pix/t3wej3CK8o3DbJrpOZKYtO6ps1UNMZ05jr0T_BxPBX0_36x36.jpg",
"width": 36,
"height": 36
}
]
},
"source": {
"name": "foursquare for iPhone",
"url": "https://foursquare.com/download/#/iphone"
},
"user": {
"id": "43",
"firstName": "christian",
"lastName": "bovine",
"photo": "https://is1.4sqi.net/userpix_thumbs/AN3FGD1WOWXA4S2F.jpg",
"gender": "male",
"homeCity": "New York, NY",
"canonicalUrl": "https://foursquare.com/xtianbovine",
"relationship": "self"
},
"visibility": "public"
}
]
},
"comments": {
"count": 0,
"items": []
},
"source": {
"name": "foursquare for iPhone",
"url": "https://foursquare.com/download/#/iphone"
}
},

最佳答案

$(function () {
var pics = [];
var json_source = 'https://api.foursquare.com/v2/users/...';
$.getJSON(json_source, function (results) {
$.each(results.response.checkins.items, function (i, item) {
if (item.photos.count > 0) {
$.each(item.photos.items, function (i, photo) {
pics.push(photo.url);
});
}
});
});
});

关于jquery - 使用 jQuery 解析 JSON 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9890385/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com