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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效