javascript - JS JSON not parsed by PHP json_decode -


i have issue parsing json data json_decode php function. issue json receive not formatted. looks follows:

"{ user:1 ,product:4 ,commentcount: 1 ,comment:'all fine! done.' ,commentdate:{ year:2015 ,month:8 ,day:19 } , likes:8 }" 

when try decode string json_decode php function null. possible format string preg_replace function

edit: found code on web wraps variable names in quotes. values still , json_decode still returns null.

// fix variable names $phpjson = preg_replace( '/([a-za-z0-9_]+?):/' , '"$1":', $phpjson ); 

working solution malformed json:

$json = "{ user:1 ,product:4 ,commentcount: 1 ,comment:'all fine! done.' ,commentdate:{ year:2015 ,month:8 ,day:19 } , likes:8 }";  $json = preg_replace('/(,|\{)[ \t\n]*(\w+)[ ]*:[ ]*/','$1"$2":',$json); $json = preg_replace('/":\'?([^\[\]\{\}]*?)\'?[ \n\t]*(,"|\}$|\]$|\}\]|\]\}|\}|\])/','":"$1"$2',$json);  var_dump($json);  var_dump(json_decode($json)); 

but in general need wrap object param in double quotes "arg":1. non-numeric values also. this:

var_dump(json_decode('{"user":1}')); var_dump(json_last_error()); 

the second function returns id of error, if there any. check php manual error codes identification


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -