I have json data represented like this
{key:"value"}
(no quotes arround key...)
I want to translate it to an associative array.
PHP's json_decode returns null
How can I add the quotes around the key?? thanks...
null is returned if the json cannot be decoded or if the encoded data is deeper than the nesting limit.
Syntax. The json_decode() function can take a JSON encoded string and convert into a PHP variable. The json_decode() function can return a value encoded in JSON in appropriate PHP type. The values true, false, and null is returned as TRUE, FALSE, and NULL respectively.
The json_decode() function is used to decode or convert a JSON object to a PHP object.
You can either fix the JSON at the source so that it returns a valid JSON structure, or you can manually add quotes around the keys.
This answer to a similar question has an example of how to do that:
function my_json_decode($s) {
$s = str_replace(
array('"', "'"),
array('\"', '"'),
$s
);
$s = preg_replace('/(\w+):/i', '"\1":', $s);
return json_decode(sprintf('{%s}', $s));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With