php method to convert string to json: first create a PHP sample file; then use the "var_dump(json_decode($json));" method to convert json.
Recommended: "PHP Video Tutorial"
php converts string to json
<?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; var_dump(json_decode($json)); var_dump(json_decode($json, true));?>
The results are
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) }
If you return null after json_decode, have you written the string like this "{ 'bar': 'baz' }", this is OK in JS It is parsed into JSON normally, but it should be written as '{ "bar": "baz" }' in PHP, and the attributes and values must use double quotes.
The above is the detailed content of How to convert string to json in php. For more information, please follow other related articles on the PHP Chinese website!