您的位置 首页 技术

php json串如何转对象

php json串转对象的方法:1、用“json_decode”对JSON格式的字符串进行编码;2、接受一个JSON格式的字符串并且把它转换为PHP变量。 php json字符串转…

php json串转对象的方法:1、用“json_decode”对JSON格式的字符串进行编码;2、接受一个JSON格式的字符串并且把它转换为PHP变量。

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/>";    }

则报错如下:

Fatal error: Cannot use objectof type stdClass as array in D:\wamp\www\test.phpon line 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($students as $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);//得到的是 objectforeach($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);//得到的是 arrayfor($i=0;$i<count($students);$i++){    echo "姓名:".$students[$i]['name']."&nbsp;&nbsp;&nbsp;年 龄:".$students[$i]['age']."&nbsp;&nbsp;&nbsp;专 业:".$students[$i]['subject']."<br />";

———————————————————————————————————————————

推荐:《PHP教程》

以上就是php json串如何转对象的详细内容,更多请关注24课堂在线网其它相关文章!

本文来自网络,不代表24小时课堂在线立场,转载请注明出处:https://www.24ketang.cn/91781.html

为您推荐

返回顶部