所以我有一个来自JSON请求的嵌套JSON响应,我在解析嵌套的JSON数据以获取某个值时遇到了麻烦,我已对需要的数据进行了注释(“ link”:“ http:/ /i.imgur.com/DnjD527h.gif“)需要在两个位置提取数据,请参阅JSON数据以获取注释。由于我对数组没有很好的了解,因此需要一些帮助来解决问题。谢谢
{
"data": {
"name": "gaming",
"display_name": "gaming",
"followers": 4264452,
"total_items": 288150,
"following": false,
"is_whitelisted": true,
"background_hash": "nrFCOUB",
"thumbnail_hash": null,
"accent": "2B1A5A",
"background_is_animated": false,
"thumbnail_is_animated": false,
"is_promoted": false,
"description": "combat and adventure",
"logo_hash": null,
"logo_destination_url": null,
"description_annotations": {},
"items": [
{
"id": "DnjD527",
"title": "Stuck between a rock and a hard place.",
"description": "Sauce: https://old.reddit.com/user/Sumit316",
"datetime": 1596212992,
"type": "image/gif",
"animated": true,
"width": 728,
"height": 408,
"size": 53069724,
"views": 50417,
"bandwidth": 2675616274908,
"vote": null,
"favorite": false,
"nsfw": false,
"section": "",
"account_url": "OctopussSevenTwo",
"account_id": 23603129,
"is_ad": false,
"in_most_viral": false,
"has_sound": false,
"tags": [
{
"name": "funny",
"display_name": "funny",
"followers": 4554003,
"total_items": 2683220,
"following": false,
"is_whitelisted": false,
"background_hash": "9r1qCDq",
"thumbnail_hash": null,
"accent": "633875",
"background_is_animated": false,
"thumbnail_is_animated": false,
"is_promoted": false,
"description": "LOLs, ROFLs, LMAOs",
"logo_hash": null,
"logo_destination_url": null,
"description_annotations": {}
},
{
"name": "gaming",
"display_name": "gaming",
"followers": 4264392,
"total_items": 288149,
"following": false,
"is_whitelisted": false,
"background_hash": "nrFCOUB",
"thumbnail_hash": null,
"accent": "2B1A5A",
"background_is_animated": false,
"thumbnail_is_animated": false,
"is_promoted": false,
"description": "combat and adventure",
"logo_hash": null,
"logo_destination_url": null,
"description_annotations": {}
},
{
"name": "reaction",
"display_name": "reaction",
"followers": 55610,
"total_items": 220184,
"following": false,
"is_whitelisted": false,
"background_hash": "UQOCajR",
"thumbnail_hash": null,
"accent": "7B74BF",
"background_is_animated": false,
"thumbnail_is_animated": false,
"is_promoted": false,
"description": "",
"logo_hash": null,
"logo_destination_url": null,
"description_annotations": {}
},
{
"name": "he_ded",
"display_name": "he ded",
"followers": 2822,
"total_items": 4594,
"following": false,
"is_whitelisted": false,
"background_hash": "QL9pTeJ",
"thumbnail_hash": null,
"accent": "159559",
"background_is_animated": false,
"thumbnail_is_animated": false,
"is_promoted": false,
"description": "",
"logo_hash": null,
"logo_destination_url": null,
"description_annotations": {}
}
],
"ad_type": 0,
"ad_url": "",
"edited": 0,
"in_gallery": true,
"topic": "No Topic",
"topic_id": 29,
"link": "http://i.imgur.com/DnjD527h.gif",/****************Need this data************/
"mp4": "https://i.imgur.com/DnjD527.mp4",
"gifv": "https://i.imgur.com/DnjD527.gifv",
"hls": "https://i.imgur.com/DnjD527.m3u8",
"mp4_size": 8025341,
"looping": true,
"processing": {
"status": "completed"
},
"ad_config": {
"safeFlags": [
"in_gallery",
"gallery"
],
"highRiskFlags": [],
"unsafeFlags": [
"sixth_mod_unsafe"
],
"wallUnsafeFlags": [],
"showsAds": false
},
"comment_count": 14,
"favorite_count": 37,
"ups": 125,
"downs": 7,
"points": 118,
"score": 142,
"is_album": false
},
{
"id": "sHoHzEI",
"title": "Almost a year in development. Here's some gameplay for you!",
"description": null,
"datetime": 1596208860,
"cover": "RS1a4kB",
"cover_width": 960,
"cover_height": 540,
"account_url": "CHILLNPLAY",
"account_id": 62468833,
"privacy": "hidden",
"layout": "blog",
"views": 2497,
"link": "https://imgur.com/a/sHoHzEI",
"ups": 105,
"downs": 2,
"points": 103,
"score": 104,
"is_album": true,
"vote": null,
"favorite": false,
"nsfw": false,
"section": "",
"comment_count": 6,
"favorite_count": 29,
"topic": "No Topic",
"topic_id": 29,
"images_count": 1,
"in_gallery": true,
"is_ad": false,
"tags": [
{
"name": "gaming",
"display_name": "gaming",
"followers": 4264431,
"total_items": 288150,
"following": false,
"is_whitelisted": false,
"background_hash": "nrFCOUB",
"thumbnail_hash": null,
"accent": "2B1A5A",
"background_is_animated": false,
"thumbnail_is_animated": false,
"is_promoted": false,
"description": "combat and adventure",
"logo_hash": null,
"logo_destination_url": null,
"description_annotations": {}
},
{
"name": "video_games",
"display_name": "video games",
"followers": 61840,
"total_items": 47453,
"following": false,
"is_whitelisted": false,
"background_hash": "XhtRLrV",
"thumbnail_hash": "XhtRLrV",
"accent": "0093BF",
"background_is_animated": false,
"thumbnail_is_animated": false,
"is_promoted": false,
"description": "",
"logo_hash": null,
"logo_destination_url": null,
"description_annotations": {}
},
{
"name": "games",
"display_name": "games",
"followers": 7735,
"total_items": 13466,
"following": false,
"is_whitelisted": false,
"background_hash": "yWpvDy6",
"thumbnail_hash": null,
"accent": null,
"background_is_animated": false,
"thumbnail_is_animated": false,
"is_promoted": false,
"description": "",
"logo_hash": null,
"logo_destination_url": null,
"description_annotations": {}
},
{
"name": "gamedev",
"display_name": "gamedev",
"followers": 3840,
"total_items": 10997,
"following": false,
"is_whitelisted": false,
"background_hash": "MEWR7Ga",
"thumbnail_hash": null,
"accent": null,
"background_is_animated": false,
"thumbnail_is_animated": false,
"is_promoted": false,
"description": "",
"logo_hash": null,
"logo_destination_url": null,
"description_annotations": {}
},
{
"name": "indiedev",
"display_name": "indiedev",
"followers": 1232,
"total_items": 4319,
"following": false,
"is_whitelisted": false,
"background_hash": "A3E5z2R",
"thumbnail_hash": null,
"accent": "24565E",
"background_is_animated": false,
"thumbnail_is_animated": false,
"is_promoted": false,
"description": "",
"logo_hash": null,
"logo_destination_url": null,
"description_annotations": {}
}
],
"ad_type": 0,
"ad_url": "",
"in_most_viral": false,
"include_album_ads": false,
"images": [
{
"id": "RS1a4kB",
"title": null,
"description": "Join the beta test:\nhttps://gamejolt.com/games/PAPER_TANKS/474788\nhttps://chillnplay.itch.io/paper-tanks\n\nFollow dev: \nhttps://twitter.com/PAPERTANKS2\nhttps://store.steampowered.com/app/1213020/PAPER_TANKS",
"datetime": 1596207639,
"type": "video/mp4",
"animated": true,
"width": 960,
"height": 540,
"size": 16796636,
"views": 61678,
"bandwidth": 1035982915208,
"vote": null,
"favorite": false,
"nsfw": null,
"section": null,
"account_url": null,
"account_id": null,
"is_ad": false,
"in_most_viral": false,
"has_sound": false,
"tags": [],
"ad_type": 0,
"ad_url": "",
"edited": "0",
"in_gallery": false,
"link": "https://i.imgur.com/RS1a4kB.mp4",/****************Need this data************/
"mp4_size": 16796636,
"mp4": "https://i.imgur.com/RS1a4kB.mp4",
"gifv": "https://i.imgur.com/RS1a4kB.gifv",
"hls": "https://i.imgur.com/RS1a4kB.m3u8",
"processing": {
"status": "completed"
},
"comment_count": null,
"favorite_count": null,
"ups": null,
"downs": null,
"points": null,
"score": null
}
],
"ad_config": {
"safeFlags": [
"in_gallery",
"sixth_mod_safe",
"album"
],
"highRiskFlags": [],
"unsafeFlags": [],
"wallUnsafeFlags": [],
"showsAds": true
}
}
]
}
"success": true,
"status": 200
}
目前这是我正在尝试的
$gifs=file_get_contents(__DIR__ ."/backend_web/Json/tags/test.txt");
$local=$gifs;
$decoded=json_decode($gifs,true);
//print_r($decoded);
foreach ($decoded as $y) {
foreach ($y->images as $z) {
$data = $z->link
}
}
但出现此错误。 注意:尝试在第29行的C:\ xampp \ htdocs \ test \ test.php中获取非对象的属性“链接” 任何帮助将不胜感激
答案 0 :(得分:1)
使用对象比使用数组更好:
$decoded=json_decode($gifs);
然后使用以下循环:
foreach ($decoded->data->items as $y) {
if (isset($y->images)) {
foreach ($y->images as $x) {
$data = $x->link;
}
}
}
某些对象没有“ images”属性,这就是为什么会出现错误。通过 isset 函数消除了。