总结:
在PHP代码中处理JSON 格式的字符串的两种方法:
方法一:
$json= '[{"id":"1","name":"u5f20u96eau6885","age":"27","subject":"u8ba1u7b97u673au79d1u5b66u4e0eu6280u672f"},{"id":"2","name":"u5f20u6c9bu9716","age":"21","subject":"u8f6fu4ef6u5de5u7a0b"}]'; $students= json_decode($json);//得到的是 object foreach($studentsas $obj){ echo "姓名:".$obj->name." 年 龄:".$obj->age." 专 业:".$obj->subject."<br />"; }
方法二:
$json= '[{"id":"1","name":"u5f20u96eau6885","age":"27","subject":"u8ba1u7b97u673au79d1u5b66u4e0eu6280u672f"},{"id":"2","name":"u5f20u6c9bu9716","age":"21","subject":"u8f6fu4ef6u5de5u7a0b"}]'; $students= json_decode($json, true);//得到的是 array for($i=0;$i<count($students);$i++){ echo "姓名:".$students[$i]['name']." 年 龄:".$students[$i]['age']." 专 业:".$students[$i]['subject']."<br />"; }