JSON string key missing quote resolution
The JSON string is a string in the form of Key:value, and the normal key is enclosed in double quotation marks .
For example:
<?php$data = Array (' name ' = ' Fdipzone '); Echo Json_encode ($data); {"Name": "Fdipzone"}print_r (Json_decode (Json_encode ($data), true)); Array ([name] = Fdipzone)?>
However, if the key of the JSON string is missing a double-quoted, then Json_decode will fail.
<?php$str = ' {' name ': ' Fdipzone '} '; Var_dump (Json_decode ($str, true)); Array (1) {["Name"]=> string (8) "Fdipzone"} $str 1 = ' {name: "Fdipzone"} '; Var_dump (Json_decode ($str 1, true)); Null?>
Workaround: Determine if there is a missing double-quoted key, if missing the first to replace with "key", then the Json_decode operation.
<?php/** compatible key does not have a double-quoted JSON string parsing * @param string $str JSON String * @param boolean $mod true:array,false:o bject* @return array/object*/function ext_json_decode ($str, $mode =false) { if (preg_match ('/\w:/', $str)) { $ str = preg_replace ('/(\w+):/is ', ' "$": ', $str); } Return Json_decode ($str, $mode);} $str = ' {' name ': ' Fdipzone '} '; Var_dump (Ext_json_decode ($str, true)); Array (1) {["Name"]=> string (8) "Fdipzone"} $str 1 = ' {name: "Fdipzone"} '; Var_dump (Ext_json_decode ($str 1, true)); Array (1) {["Name"]=> string (8) "Fdipzone"}?>
This article explains the solution to the lack of double quotes for JSON string key, and more about the PHP Chinese web.
Related recommendations:
Explanation of MySQL strict mode Strict modes
PHP uses explode split string for beginners easy to ignore problems explained
Explanation of two-column data methods in MySQL interchange table