我有JSON:{ "type": "donut", "name": "Cake", "toppings": [ { "id": "5002", "type": "Glazed" }, { "id": "5006", "type": "Chocolate with Sprinkles" }, { "id": "5004", "type": "Maple" } ]}如何在PHP中解码并访问结果数据?
2 回答
狐的传说
TA贡献1804条经验 获得超3个赞
您可以使用json_decode()将json字符串转换为PHP对象/数组。
例如。
输入:
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';var_dump(json_decode($json));var_dump(json_decode($json, true));输出:
object(stdClass)#1 (5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)}array(5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)}几点要记住:
json_decode要求字符串是有效的,json否则它将返回NULL。如果解码失败,
json_last_error()可用于确定错误的确切性质。确保传入
utf8内容,或者json_decode可能出错并只返回一个NULL值。
- 2 回答
- 0 关注
- 3368 浏览
添加回答
举报
0/150
提交
取消
