php将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后返回null,你是不是把字符串写成这样了"{ 'bar': 'baz' }",这个在JS里是可以正常解析成JSON的,但是PHP里面要写成'{ "bar": "baz" }',属性和值要用双引号。
PHP函数json_encode()如何将数据变量转成json