嗨,我在Kodi Player中遇到json响应问题,我需要提取两个值,例如channelid和label,我需要在PHP 5.2中完成
{"id":"libGetChannels","jsonrpc":"2.0","result":{"channels":[{"channel":"BBC
One HD","channelid":3,"label":"BBC One HD","lastplayed":"2018-08-16"},
{"channel":"BBC Two HD","channelid":4,"label":"BBC Two
HD","lastplayed":"2018-08-16"}],"limits":{"end":2,"start":0,"total":2}}}
感谢我的帮助,我是Json的新人
答案 0 :(得分:0)
$json = '{"id":"libGetChannels","jsonrpc":"2.0","result":{"channels":[{"channel":"BBC One HD","channelid":3,"label":"BBC One HD","lastplayed":"2018-08-16"},{"channel":"BBC Two HD","channelid":4,"label":"BBC Two HD","lastplayed":"2018-08-16"}],"limits":{"end":2,"start":0,"total":2}}}';
$array = json_decode($json);
$channelsArray = [];
foreach ($array as $value){
if(is_object($value)){
foreach ($value->channels as $channel){
$channelsArray[] = $channel;
}
}
}
foreach($channelsArray as $channel){
echo $channel->channelid;
echo $channel->label;
}