doukang7486 2017-01-12 16:45
浏览 59
已采纳

JSON解码,Parse JSON响应和整数值的Undefined Index

It's so weird that I have been working on this for three days looking around the web to find a solution and making my own changes but still have no luck. I'm trying to retrieve amazon products into my website with Laravel and this great package from JoeDawson/amazon-ecs. when I execute the command from the controller like this below:

$results = Amazon::search('tv')->json();
dd($results)

I can see all the data from Amazon like this:

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"
          ]
      ]
    ]
  ]
]

Or the entire result is here: http://pastebin.com/TGFgCbAz

In my view, I have access to all the values except the values inside ListPrice, which gives back "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

I tried the same thing on the controller side:

foreach ($amazon_results['Items']['Item'] as $amazon) {
        print_r($amazon['ItemAttributes']['ListPrice']['FormattedPrice']);
    }

This returns the values of FormattedPrice first then throws the same error.

£199.99£34.99£194.50 Whoops, looks like something went wrong.

1/1 ErrorException in HomeController.php line 24: Undefined index: ListPrice

Why am I not able to get this value?

  • 写回答

1条回答 默认 最新

  • dongmang3961 2017-01-13 18:33
    关注

    Hate to be the bearer of bad news, but it's simply that there is no ListPrice array for all the items. The full dump you provided had ten items. Two of them (index 7 and index 9) did not contain the ListPrice array. I'd suggest checking to make sure it exists first:

    @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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?