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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)