PHP 如何循环 JSON 和提取字段
PHP how to loop JSON and Extract fields
我正在使用 PHP 和 curl 从航班旅行中提取数据 API
我正在尝试循环旅行 API 的以下 JSON 输出,并尝试使用循环将“flight_date”和“机场”填充到变量中。
{
"pagination": {
"limit": 1,
"offset": 0
},
"data": [
{
"flight_date": "2021-12-19",
"flight_status": "scheduled",
"departure": {
"airport": "Suvarnabhumi International",
"timezone": "Asia/Bangkok"
},
"arrival": {
"airport": "Seoul (Incheon)",
"timezone": "Asia/Seoul",
"iata": "ICN"
},
"airline": {
"name": "Delta Air Lines"
},
"flight": {
"number": "7918",
"iata": "DL7918"
"codeshared": {
"airline_name": "korean air",
"airline_iata": "ke"
}
},
"aircraft": null,
"live": null
}
]
}
当我循环上面的内容时,我一直在获取数组以获取数组到字符串的转换
为了将 JSON 用作 PHP 数组,您实际需要做的是使用 json_decode().
您还需要先确保您的 JSON 没有畸形。我试图像这样调试提供的 JSON:
$json[] = '{
"pagination": {
"limit": 2,
"offset": 0,
"count": 2,
"total": 315303
},
"data": [
{
"flight_date": "2021-12-19",
"flight_status": "scheduled",
"departure": {
"airport": "Suvarnabhumi International",
"timezone": "Asia/Bangkok",
},
"arrival": {
"airport": "Seoul (Incheon)",
"timezone": "Asia/Seoul",
},
"airline": {
"name": "Delta Air Lines",
"iata": "DL",
"icao": "DAL"
},
"flight": {
"number": "7918",
"iata": "DL7918",
}
},
"aircraft": null,
"live": null
},
{
"flight_date": "2021-12-19",
"flight_status": "scheduled",
"departure": {
"airport": "Suvarnabhumi International",
"timezone": "Asia/Bangkok",
},
"arrival": {
"airport": "Heathrow",
"timezone": "Europe/London",
},
"airline": {
"name": "Air Canada",
"iata": "AC",
"icao": "ACA"
},
"flight": {
"number": "6123",
"iata": "AC6123",
}
},
"aircraft": null,
"live": null
}
]
}';
foreach ($json as $string) {
echo 'Decoding: ' . $string;
json_decode($string);
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
echo PHP_EOL;
}
您的 JSON 格式似乎有误。
您可以使用此站点:https://jsonlint.com/ 检查您的 json.
问题出在哪里
我正在使用 PHP 和 curl 从航班旅行中提取数据 API 我正在尝试循环旅行 API 的以下 JSON 输出,并尝试使用循环将“flight_date”和“机场”填充到变量中。
{
"pagination": {
"limit": 1,
"offset": 0
},
"data": [
{
"flight_date": "2021-12-19",
"flight_status": "scheduled",
"departure": {
"airport": "Suvarnabhumi International",
"timezone": "Asia/Bangkok"
},
"arrival": {
"airport": "Seoul (Incheon)",
"timezone": "Asia/Seoul",
"iata": "ICN"
},
"airline": {
"name": "Delta Air Lines"
},
"flight": {
"number": "7918",
"iata": "DL7918"
"codeshared": {
"airline_name": "korean air",
"airline_iata": "ke"
}
},
"aircraft": null,
"live": null
}
]
}
当我循环上面的内容时,我一直在获取数组以获取数组到字符串的转换
为了将 JSON 用作 PHP 数组,您实际需要做的是使用 json_decode().
您还需要先确保您的 JSON 没有畸形。我试图像这样调试提供的 JSON:
$json[] = '{
"pagination": {
"limit": 2,
"offset": 0,
"count": 2,
"total": 315303
},
"data": [
{
"flight_date": "2021-12-19",
"flight_status": "scheduled",
"departure": {
"airport": "Suvarnabhumi International",
"timezone": "Asia/Bangkok",
},
"arrival": {
"airport": "Seoul (Incheon)",
"timezone": "Asia/Seoul",
},
"airline": {
"name": "Delta Air Lines",
"iata": "DL",
"icao": "DAL"
},
"flight": {
"number": "7918",
"iata": "DL7918",
}
},
"aircraft": null,
"live": null
},
{
"flight_date": "2021-12-19",
"flight_status": "scheduled",
"departure": {
"airport": "Suvarnabhumi International",
"timezone": "Asia/Bangkok",
},
"arrival": {
"airport": "Heathrow",
"timezone": "Europe/London",
},
"airline": {
"name": "Air Canada",
"iata": "AC",
"icao": "ACA"
},
"flight": {
"number": "6123",
"iata": "AC6123",
}
},
"aircraft": null,
"live": null
}
]
}';
foreach ($json as $string) {
echo 'Decoding: ' . $string;
json_decode($string);
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
echo PHP_EOL;
}
您的 JSON 格式似乎有误。 您可以使用此站点:https://jsonlint.com/ 检查您的 json.
问题出在哪里