【发布时间】: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"
}
]
}
}
我想在循环中获取嵌套数组数据 name 和 iconUrl 但似乎 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;
}
【问题讨论】: