比较复杂的JSON格式转换数组后如何提取值

标签: 

正在查看 0 条回复
  • 作者
    帖子
    • Frank Chen - WirelessLink社区Frank Chen
      版主
      #1427
      Up
      4
      Down
      ::

      一般比较复杂的业务场景会用到json嵌套,通过json_decode将json格式数据转换为数组格式后,该如何调用呢?

      $RG_array = json_decode($json_body,true);
      
      //-debug code
      /*echo'参数RG_array';
      var_dump($RG_array);
      */

       

      1>先通过var_dump函数将数组变量打印出来,进行格式化操作后得到如下的数组

      array(7) {
      ["authenticationResultCode"]=> string(16) "ValidCredentials"
      ["brandLogoUri"]=> string(56) "http://xxx"
      ["copyright"]=> string(247) "Copyright © 2022 "
      ["resourceSets"]=> array(1) {
           [0]=> array(2) {
      ["estimatedTotal"]=> int(1)
                      ["resources"]=> array(1) {
                      [0]=> array(9) {
      ["__type"]=> string(61) "xxxx"
      ["bbox"]=> array(4) {
      [0]=> float(xxxx.xx)
      [1]=> float(xxxx.xx)
      }
      
      ["name"]=> string(56) "xxxxxx"
      ["point"]=> array(2) {
      ["type"]=> string(5) "Point"
      ["coordinates"]=> array(2) {
      [0]=> float(xxxx.xx)
      [1]=> float(xxxx.xx)
      }
      }
      
      ["address"]=> array(6) {
      ["addressLine"]=> string(13) "xxxx"
      ["adminDistrict"]=> string(8) "xxxx"
      ["adminDistrict2"]=> string(8) "xxxx"
      ["countryRegion"]=> string(5) "xxxx"
      ["formattedAddress"]=> string(56) "xxxx"
      ["locality"]=> string(15) "xxxx"
      }
      
      ["confidence"]=> string(4) "High"
      ["geocodePoints"]=> array(1) {
      [0]=> array(4) {
      ["type"]=> string(5) "Point"
      ["coordinates"]=> array(2) {
      [0]=> float(xxxx.xx)
      [1]=> float(xxxx.xx)
      }
      ["calculationMethod"]=> string(7) "Rooftop"
      ["usageTypes"]=> array(1) {
      [0]=> string(7) "Display"
      }
      }
      }
      
      ["matchCodes"]=> array(1) {
      [0]=> string(4) "Good"
      }
      }
      }
      }
      }
      
      ["statusCode"]=> int(200)
      ["statusDescription"]=> string(2) "OK"
      ["traceId"]=> string(51) "xxxx|CO00004BC9|0.0.0.1"
      }

       

       

      2> 定义数组提取参数。以上面的参数为例,这里我们想提取数组里面的formattedAddress 参数值

      //详细地址信息数组.一维数组.注意json大括号前面的键值0
      $local_regeocode=$RG_array['resourceSets'][0];
      //详细地址信息的数组.二维数组
      $resources= $local_regeocode['resources'][0];
      $address = $resources['address'];
      //已经格式化后的数据.三位数组
      $real_address =$address['formattedAddress'];

       

      结束。

       

      访问ChatGPT的可用VPS机房IP推荐 Lisahost美国原生IP搬瓦工美西DMITTripodcloudFrantech
正在查看 0 条回复
  • 哎呀,回复话题必需登录。
WirelessLink社区
Logo