json_en code和json_de code的使用 [英] Usage of json_encode and json_decode

查看:1050
本文介绍了json_en code和json_de code的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的PHP。我使用json_en code到一个数组转换成JSON数据,并使用json_de code德code那么它在另一个文件中。但是,我得到的JSON误差语法错误。

我的code是如下:

文件1:

  $结果= get_data_array();出口(json_en code($结果));

文件2:

  $结果=的file_get_contents(HTTP://localhost/file1.php);
$数据= json_de code($结果,真正的);$数据 - > //名字的名字是数组关键

不过,我得到一个错误的:


  

试图让非对象的属性。



解决方案

您传递真实的 json_de code 所以它会返回一个数组。

使用这样的:

  $结果=的file_get_contents(HTTP://localhost/file1.php);
$数据= json_de code($结果,真正的);
回声$数据['名'];

I am new to PHP. I am using json_encode to convert an array into json data, and decode it using json_decode in another file. However, I am getting json error as syntax error.

My code is as follows:

File 1:

$result = get_data_array();

exit(json_encode($result));

File 2:

$result = file_get_contents("http://localhost/file1.php");
$data = json_decode($result,true);

$data->name // name is the array key

However, I am getting an error as:

Trying to get property of non-object.

解决方案

You passed true to the second parameter of json_decode so it will return an array.

Use this:

$result = file_get_contents("http://localhost/file1.php"); 
$data = json_decode($result,true);
echo $data['name'];

这篇关于json_en code和json_de code的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆