JSON 解码、解析 JSON 响应和整数值的未定义索引
JSON decode, Parse JSON response and Undefined Index on integer value
这太奇怪了,我已经为此工作了三天,在网上四处寻找解决方案并进行了自己的更改,但仍然没有运气。我正在尝试使用 Laravel 和来自 JoeDawson/amazon-ecs 的这个很棒的包裹将亚马逊产品检索到我的网站。
当我像下面这样从控制器执行命令时:
$results = Amazon::search('tv')->json();
dd($results)
我可以这样查看来自亚马逊的所有数据:
array:2 [
"OperationRequest" => array:4 [
...
]
"Items" => array:5 [
...
"Item" => array:10 [
...
"ItemAttributes" => array:22 [
"Binding" => "Electronics"
"Brand" => "LG Electronics"
"Color" => "Black"
"EAN" => "8806087769050"
"EANList" => array:1 [
"EANListElement" => "8806087769050"
]
"Feature" => array:4 [
0 => "High dynamic contrast ratio (5M:1) - richer colors, deeper blacks and greater depth of image"
1 => "Gaming and cinema modes - dedicated features to optimise viewing experiences"
2 => "USB AutoRun - media content from USB stick runs automatically as soon as TV is switched on"
3 => "Detachable base - easy way to wall mount your TV and enjoy a viewing experience while saving space"
]
"ItemDimensions" => array:4 [
"Height" => "226"
"Length" => "1560"
"Weight" => "948"
"Width" => "2526"
]
"Label" => "LG"
"ListPrice" => array:3 [
"Amount" => "19999"
"CurrencyCode" => "GBP"
"FormattedPrice" => "£199.99"
]
]
]
]
]
或者整个结果在这里:http://pastebin.com/TGFgCbAz
在我看来,我可以访问除 ListPrice
内的值以外的所有值,这会返回 "Undefined Index"
@foreach ($results['Items']['Item'] as $amazon)
{{ $amazon['ItemAttributes']['Title'] }} //returns true with the value
{{ $amazon['ItemAttributes']['ListPrice']['FormattedPrice'] }} //returns Undefined index: ListPrice
@endforeach
我在控制器端尝试了同样的事情:
foreach ($amazon_results['Items']['Item'] as $amazon) {
print_r($amazon['ItemAttributes']['ListPrice']['FormattedPrice']);
}
这 returns FormattedPrice 的值首先抛出相同的错误。
£199.99£34.99£194.50 Whoops, looks like something went wrong.
1/1 ErrorException in HomeController.php line 24: Undefined index:
ListPrice
为什么我无法获得这个值?
讨厌成为坏消息的传递者,但只是没有 ListPrice
数组包含所有项目。您提供的完整转储有十项。其中两个(索引 7 和索引 9)不包含 ListPrice
数组。我建议先检查以确保它存在:
@foreach ($results['Items']['Item'] as $amazon)
{{ $amazon['ItemAttributes']['Title'] }}
@if (array_key_exists('ListPrice', $amazon['ItemAttributes']))
{{ $amazon['ItemAttributes']['ListPrice']['FormattedPrice'] }}
@else
No list price
@endif
@endforeach
这太奇怪了,我已经为此工作了三天,在网上四处寻找解决方案并进行了自己的更改,但仍然没有运气。我正在尝试使用 Laravel 和来自 JoeDawson/amazon-ecs 的这个很棒的包裹将亚马逊产品检索到我的网站。 当我像下面这样从控制器执行命令时:
$results = Amazon::search('tv')->json();
dd($results)
我可以这样查看来自亚马逊的所有数据:
array:2 [
"OperationRequest" => array:4 [
...
]
"Items" => array:5 [
...
"Item" => array:10 [
...
"ItemAttributes" => array:22 [
"Binding" => "Electronics"
"Brand" => "LG Electronics"
"Color" => "Black"
"EAN" => "8806087769050"
"EANList" => array:1 [
"EANListElement" => "8806087769050"
]
"Feature" => array:4 [
0 => "High dynamic contrast ratio (5M:1) - richer colors, deeper blacks and greater depth of image"
1 => "Gaming and cinema modes - dedicated features to optimise viewing experiences"
2 => "USB AutoRun - media content from USB stick runs automatically as soon as TV is switched on"
3 => "Detachable base - easy way to wall mount your TV and enjoy a viewing experience while saving space"
]
"ItemDimensions" => array:4 [
"Height" => "226"
"Length" => "1560"
"Weight" => "948"
"Width" => "2526"
]
"Label" => "LG"
"ListPrice" => array:3 [
"Amount" => "19999"
"CurrencyCode" => "GBP"
"FormattedPrice" => "£199.99"
]
]
]
]
]
或者整个结果在这里:http://pastebin.com/TGFgCbAz
在我看来,我可以访问除 ListPrice
内的值以外的所有值,这会返回 "Undefined Index"
@foreach ($results['Items']['Item'] as $amazon)
{{ $amazon['ItemAttributes']['Title'] }} //returns true with the value
{{ $amazon['ItemAttributes']['ListPrice']['FormattedPrice'] }} //returns Undefined index: ListPrice
@endforeach
我在控制器端尝试了同样的事情:
foreach ($amazon_results['Items']['Item'] as $amazon) {
print_r($amazon['ItemAttributes']['ListPrice']['FormattedPrice']);
}
这 returns FormattedPrice 的值首先抛出相同的错误。
£199.99£34.99£194.50 Whoops, looks like something went wrong.
1/1 ErrorException in HomeController.php line 24: Undefined index: ListPrice
为什么我无法获得这个值?
讨厌成为坏消息的传递者,但只是没有 ListPrice
数组包含所有项目。您提供的完整转储有十项。其中两个(索引 7 和索引 9)不包含 ListPrice
数组。我建议先检查以确保它存在:
@foreach ($results['Items']['Item'] as $amazon)
{{ $amazon['ItemAttributes']['Title'] }}
@if (array_key_exists('ListPrice', $amazon['ItemAttributes']))
{{ $amazon['ItemAttributes']['ListPrice']['FormattedPrice'] }}
@else
No list price
@endif
@endforeach