php中json如何转为对象(json,php,编程语言)

时间:2024-05-03 15:11:53 作者 : 石家庄SEO 分类 : 编程语言
  • TAG :

php json转为对象的方法:首先创建一个PHP示例文件;然后用“get_object_vars”把类的类型转换成数组; 最后用foreach遍历即可。

php json字符串转为数组或对象

从网上查到的方法是 用get_object_vars 把类类型转换成数组 然后在用foreach 遍历即可

$array=get_object_vars($test);
$json='[{"id":"1","name":"\u5f20\u96ea\u6885","age":"27","subject":"\u8ba1\u7b97\u673a\u79d1\u5b66\u4e0e\u6280\u672f"},{"id":"2","name":"\u5f20\u6c9b\u9716","age":"21","subject":"\u8f6f\u4ef6\u5de5\u7a0b"}]';

首先要用 json_decode 对 JSON 格式的字符串进行编码,

$students=json_decode($json);

直接在PHP文件用$students :

for($i=0;$i<count($students);$i++){
echo"姓名:".$students[$i]['name']."年龄:".$students[$i]['age']."专业:".$students[$i]['subject']."<br/>";
}

则报错如下:

Fatalerror
:CannotuseobjectoftypestdClassasarrayin
D:\wamp\www\test.php
online
18

这时候打印一下 $students :

var_dump($students);

会输出:

array(2){
[0]=>
object(stdClass)#2(4){
["id"]=>string(1)"1"
["name"]=>string(9)"张雪梅"
["age"]=>string(2)"27"
object(stdClass)#3(4){这个就说明转换的json字符串转为对象而非数组,请看下面的红色背景字
["subject"]=>string(24)"计算机科学与技术"
}
[1]=>
["id"]=>string(1)"2"
["name"]=>string(9)"张沛霖"
["age"]=>string(2)"21"
["subject"]=>string(12)"软件工程"
}
}

可见,返回的结果是 object 而非 array。应以对象形式访问:

foreach($studentsas$obj){
echo"姓名:".$obj->name."年龄:".$obj->age."专业:".$obj->subject."<br/>";
}

输出结果为:

姓名:张雪梅 年龄:27 专业:计算机科学与技术
姓名:张沛霖 年龄:21 专业:软件工程

mixedjson_decode ( string$json [, bool$assoc ] )

说明:接受一个 JSON 格式的字符串并且把它转换为 PHP 变量。【】

json_decode 可接收两个参数:

json:待解码的jsonstring 格式的字符串。

assoc:当该参数为 TRUE 时,将返回 array 而非 object 。

$students=json_decode($json,true);

这时打印一下 $students :

var_dump($students);

输出:

array(2){
[0]=>
array(4){
["id"]=>string(1)"1"
["name"]=>string(9)"张雪梅"
["age"]=>string(2)"27"
["subject"]=>string(24)"计算机科学与技术"
}
[1]=>
array(4){
["id"]=>string(1)"2"
["name"]=>string(9)"张沛霖"
["age"]=>string(2)"21"
["subject"]=>string(12)"软件工程"
}
}

这时,$students 就是个数组了,可以直接用:

for($i=0;$i<count($students);$i++){
echo"姓名:".$students[$i]['name']."年龄:".$students[$i]['age']."专业:".$students[$i]['subject']."<br/>";
}

输出结果为:

姓名:张雪梅 年龄:27 专业:计算机科学与技术
姓名:张沛霖 年龄:21 专业:软件工程

总结:

在PHP代码中处理JSON 格式的字符串的两种方法:

方法一:

$json='[{"id":"1","name":"\u5f20\u96ea\u6885","age":"27","subject":"\u8ba1\u7b97\u673a\u79d1\u5b66\u4e0e\u6280\u672f"},{"id":"2","name":"\u5f20\u6c9b\u9716","age":"21","subject":"\u8f6f\u4ef6\u5de5\u7a0b"}]';
$students=json_decode($json);//得到的是object
foreach($studentsas$obj){
echo"姓名:".$obj->name."&nbsp;&nbsp;&nbsp;年龄:".$obj->age."&nbsp;&nbsp;&nbsp;专业:".$obj->subject."<br/>";}

方法二:

$json='[{"id":"1","name":"\u5f20\u96ea\u6885","age":"27","subject":"\u8ba1\u7b97\u673a\u79d1\u5b66\u4e0e\u6280\u672f"},{"id":"2","name":"\u5f20\u6c9b\u9716","age":"21","subject":"\u8f6f\u4ef6\u5de5\u7a0b"}]';
$students=json_decode($json,true);//得到的是array
for($i=0;$i<count($students);$i++){echo"姓名:".$students[$i]['name']."&nbsp;&nbsp;&nbsp;年龄:".$students[$i]['age']."&nbsp;&nbsp;&nbsp;专业:".$students[$i]['subject']."<br/>";

---------------------------------------------------------------------------------------------------------------------------------

php中json如何转为对象

php中json如何转为对象

php中json如何转为对象

希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

本文:php中json如何转为对象的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:react fiber执行原理是什么下一篇:

10 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18