使用PHP处理JSON嵌套数组中JSON嵌套数组的项



注意,这类似于用PHP处理多维JSON数组,只使用一个caviot。

我有一个JSON,里面有一些数组,来自Airtable(URL已审查(

{
"records": [
{
"id": "RECORD_ID",
"fields": {
"Name": "Setsuna Meioh",
"Birthday": "2020-10-29",
"Gender": "Female",
"Series": "Sailor Moon",
"Picture": [
{
"id": "this_is_id1",
"url": "http://IMAGE.URL",
"filename": "IMAGE_FILENAME",
"size": 97060,
"type": "image/png",
"thumbnails": {
"small": {
"url": "https://dl.airtable.com/THUMBNAIL_SMAL_URL",
"width": 34,
"height": 36
},
"large": {
"url": "https://dl.airtable.com/THUMBNAIL_LARGE_URL",
"width": 295,
"height": 317
},
"full": {
"url": "https://dl.airtable.com/THUMBNAIL_FULL_URL",
"width": 3000,
"height": 3000
}
}
}
]
}
},
{
"id": "RECORD_ID2",
"fields": {
"Name": "Rin Hoshizora",
"Birthday": "2020-11-01",
"Gender": "Female",
"Series": "Love Live (School Idol Project)",
"Picture": [
{
"id": "this_is_id2",
"url": "http://IMAGE.URL",
"filename": "IMAGE_FILENAME",
"size": 131749,
"type": "image/png",
"thumbnails": {
"small": {
"url": "https://dl.airtable.com/THUMBNAIL_SMAL_URL",
"width": 34,
"height": 36
},
"large": {
"url": "https://dl.airtable.com/THUMBNAIL_LARGE_URL",
"width": 295,
"height": 317
},
"full": {
"url": "https://dl.airtable.com/THUMBNAIL_FULL_URL",
"width": 3000,
"height": 3000
}
}
}
]
}
}
]
}

我正在尝试访问图片数组中的url项,并且我已经通过cURL响应设置了jsonDecode方法。

现在,我已经知道如何在访问数组时使用括号[](如上面的链接所示(,但问题是,数组中还有另一个数组。

我所做的是使用一个较长的字符串来获取一个数组。

echo $data->records[0]->fields->Picture[0]->thumbnails->small->url;

我试过了,但不幸的是一无所获。然后我尝试了下面的另一种方法

$Image1Base = $data->records[0]->fields->Picture[0];
$Image1URL = $Image1Base->url;
// then the echo
echo $Image1URL

也没什么结果。有什么东西不见了吗?

它是有效的。

检查是否存在其他错误。

实际数据中是否不存在某种预期结构。

%cat test4.php

<?php
$jsonStr =
'{
"records": [
{
"id": "RECORD_ID",
"fields": {
"Name": "Setsuna Meioh",
"Birthday": "2020-10-29",
"Gender": "Female",
"Series": "Sailor Moon",
"Picture": [
{
"id": "this_is_id1",
"url": "http://IMAGE.URL",
"filename": "IMAGE_FILENAME",
"size": 97060,
"type": "image/png",
"thumbnails": {
"small": {
"url": "https://dl.airtable.com/THUMBNAIL_SMAL_URL",
"width": 34,
"height": 36
},
"large": {
"url": "https://dl.airtable.com/THUMBNAIL_LARGE_URL",
"width": 295,
"height": 317
},
"full": {
"url": "https://dl.airtable.com/THUMBNAIL_FULL_URL",
"width": 3000,
"height": 3000
}
}
}
]
}
},
{
"id": "RECORD_ID2",
"fields": {
"Name": "Rin Hoshizora",
"Birthday": "2020-11-01",
"Gender": "Female",
"Series": "Love Live (School Idol Project)",
"Picture": [
{
"id": "this_is_id2",
"url": "http://IMAGE.URL",
"filename": "IMAGE_FILENAME",
"size": 131749,
"type": "image/png",
"thumbnails": {
"small": {
"url": "https://dl.airtable.com/THUMBNAIL_SMAL_URL",
"width": 34,
"height": 36
},
"large": {
"url": "https://dl.airtable.com/THUMBNAIL_LARGE_URL",
"width": 295,
"height": 317
},
"full": {
"url": "https://dl.airtable.com/THUMBNAIL_FULL_URL",
"width": 3000,
"height": 3000
}
}
}
]
}
}
]
}';
$data = json_decode($jsonStr);
$Image1Base = $data->records[0]->fields->Picture[0];
$Image1URL = $Image1Base->url;
var_dump($Image1URL);

%php-S localhost:8001

view-source:http://localhost:8001/test4.php

string(16) "http://IMAGE.URL"

最新更新