This article describes the PHP processing JSON string key is missing the quotation mark solution, share for everyone to use for reference. The specific methods are as follows:
In general, the JSON string is a key:value-form string, and the normal key is enclosed in double quotes.
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 double quotes, 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
?>
Solution: To determine whether there is a lack of dual-cited key, such as the absence of the first replacement with the "key", and then Json_decode operation.
<?php
/** compatible key has no dual-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 '}
?>
I hope this article will help you with the learning of PHP programming.