【问题标题】:json_decode api string to get nested array data [duplicate]json_decode api字符串以获取嵌套数组数据[重复]
【发布时间】:2021-08-10 02:32:13
【问题描述】:

我有这个来自银行的 JSON API,调用时会返回类似的内容

{
   "AllBillerListRes":{
      "version":"2.3",
      "resDesc":"Success",
      "resDescTh":"สำเร็จ",
      "resDescLocale":"Success",
      "biller":[
         {
            "id":"226",
            "nameEn":"DTAC Online",
            "nameTh":"ดีแทคออนไลน์",
            "nameLocale":"DTAC Online",
            "nameLocaleList":{
               "en":"DTAC Online",
               "th":"ดีแทคออนไลน์",
               "my":"DTAC Online"
            },
            "iconUrl":"https://226.com"
         },
         {
            "id":"249",
            "nameEn":"Link",
            "nameTh":"Link",
            "nameLocale":"Link",
            "nameLocaleList":{
               "en":"Link",
               "th":"Link",
               "my":"Link"
            },
            "iconUrl":"https://249.com"
         }
      ]
   }
}   

我想在循环中获取嵌套数组数据 nameiconUrl 但似乎 AllBillerListRes 不是数组,因为它不是没有 [ 是否有任何其他方法可以 json_decode 或者我可以用这样的方法来做到这一点

$decoded = json_decode($json_string, true);
$biller = $decoded->AllBillerListRes[0]->biller;
foreach($biller as $biller)
  {
    $name = $biller->nameEn;
    $icon = $biller->iconUrl;
    echo '<br> Name ->' .$name .'<br> IconURL ->' .$icon; 
  }

【问题讨论】:

    标签: php arrays json


    【解决方案1】:

    它确实可以解决这个问题,问题是 AllBillerListRes 无法获取索引但可以获取嵌套数组。

    foreach($decoded['AllBillerListRes']['biller'] as $bill)
        {
          $name = $bill['nameEn'];
          $icon = $bill['iconUrl'];
          echo '<br> Name ->' .$name .'<br> IconURL ->' .$icon; 
        }
    

    【讨论】:

      猜你喜欢
      • 2022-01-09
      • 2019-08-27
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多