[英]JSON - PHP foreach persistant error
我对此感到非常沮丧,可以使用一些精明的头脑。 正在构建一个相对简单的 API。 使用 PHP 创建了 stdClass() 和 json_encode。 在主机服务器上,数据完美地回响。 在客户端,我收到一个持续的 foreach 无效参数错误。
$thefez= new stdClass();
$thefez->muid=$id;
$thefez->bandname=$bandname;
$thefez->core=new stdClass();
$thefez->core->joined=$since;
$thefez->core->bandbio=$bio;
$thefez->core->genre=$genre;
$thefez->core->subgenre=$subgenre;
echo json_encode($thefez);
结果(主持人)
{"muid":"IM5LGM02MFS8RJLKGY9W","bandname":"Marbles For Zen","core":
{"joined":"Sun 01 March 2015","bandbio":"Zen And Marbles","genre":"Rhythm Blues",
"subgenre":"Dixie Rhythm"}}
{"muid":"IMA3YNBKZQDNR9RBCSRI","bandname":"Frankie Storm","core":
{"joined":"Sat 21 February 2015","bandbio":"Just registered. Bio coming soon.","genre":"Popular","subgenre":""}}
问题:使用 json_decode 和 foreach 只是想回显数组中的项目。
json_decode(file_get_contents('http://api.mutrs.me/?artists'), TRUE);
foreach($result as $item){
$item->muid;
}
主持人:
检查 json_last_error 它返回 0
检查 json_last_error_msg 它返回没有错误
客户:
检查 json_last_error 它返回4
检查 json_last_error_msg 它返回语法错误
jsondecode 转换为数组而不是标准类对象
先试试这个$result = json_decode($object , true);
然后为您的项目$varname = $item['muid'];
与您的代码相比,对此进行测试:
<?php
$data = '{"muid":"IM5LGM02MFS8RJLKGY9W","bandname":"Marbles For Zen","core":{"joined":"Sun 01 March 2015","bandbio":"Zen And Marbles","genre":"Rhythm Blues","subgenre":"Dixie Rhythm"}}';
var_dump(json_decode($data , true));
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.