[英]Usage of json_encode and json_decode
我是PHP新手。 我正在使用json_encode将数组转换为json数据,并在另一个文件中使用json_decode对其进行解码。 但是,我得到json错误作为语法错误。
我的代码如下:
文件1:
$result = get_data_array();
exit(json_encode($result));
档案2:
$result = file_get_contents("http://localhost/file1.php");
$data = json_decode($result,true);
$data->name // name is the array key
但是,我收到以下错误消息:
试图获取非对象的属性。
您将true传递给json_decode的第二个参数,因此它将返回一个数组。
用这个:
$result = file_get_contents("http://localhost/file1.php");
$data = json_decode($result,true);
echo $data['name'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.