I'm pulling a JSON feed that is invalid JSON. It's missing quotes entirely. I've tried a few things, like explode()
and str_replace()
, to get the string looking a little bit more like valid JSON, but with an associate JSON string inside, it generally gets screwed up.
Here's an example:
id:43015,name:'John Doe',level:15,systems:[{t:6,glr:1242,n:'server',s:185,c:9}],classs:0,subclass:5
Are there any JSON parsers for php out there that can handle invalid JSON like this?
Edit: I'm trying to use json_decode()
on this string. It returns nothing.
"
and not single quotes '
.function my_json_decode($s) { $s = str_replace( array('"', "'"), array('\"', '"'), $s ); $s = preg_replace('/(\w+):/i', '"\1":', $s); return json_decode(sprintf('{%s}', $s)); }
This regex will do the trick
$json = preg_replace('/([{,])(\s*)([A-Za-z0-9_\-]+?)\s*:/','$1"$3":',$json);
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