doubi4435 2015-06-16 10:12
浏览 63
已采纳

json响应处理问题

I'm stuck with retrieving json response below is the json output. Your help would be highly appreciated.

{ "productHeader" : { "totalHits" : 684 }, "products" : [ { "name" : "Victoria Hotels", "productImage" : { "url" : "http://hotels.com/hotels/9000000/8640000/8633700/8633672/8633672_20_b.jpg" }, "language" : "en", "description" : "Location. Victoria Hotels is in Foshan (Nanhai) and area attractions include Renshou Temple and New Plaza Stadium. Additional regional attractions include Guangdong Folk Art Museum and Bright Filial Piety Temple.", "identifiers" : { }, "fields" : [ { "name" : "regions2", "value" : "Guangdong" }, 

Please help me to fetch the particular values. For example if I need to fetch the name, image url from the json response.

  • 写回答

2条回答 默认 最新

  • dsoxcj7276 2015-06-16 10:21
    关注

    You can use json_decode to parse a JSON string to an array and access it's values:

    // assuming, that $string contains the json response
    // second parameter to true, to get an array instead of an object
    $data = json_decode( $string, true );
    if ( $data ) {
      echo $data['products'][0]['name'];
      // or whatever value
    } else {
      echo 'JSON could not be parsed, error: ' . json_last_error();
    }
    

    To show all values in the products array, simple loop it:

    if ( $data ) {
      foreach($data['products'] as $product){
          echo $product['name'];
      }
      // or whatever value
    } else {...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?