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.
From my experience Marko's answer doesnt work anymore. For newer php versions use this istead:
I'd say your best bet is to download the source of a JSON decoder (they're not huge) and fiddle with it, especially if you know what's wrong with the JSON you're trying to decode.
The example you provided needs { } around it, too, which may help.
"
and not single quotes'
.I know this question is old, but I hope this helps someone.
I had a similar problem, in that I wanted to accept JSON as a user input, but didn't want to require tedious "quotes" around every key. Furthermore, I didn't want to require quotes around the values either, but still parse valid numbers.
The simplest way seemed to be writing a custom parser.
I came up with this, which parses to nested associative / indexed arrays:
Note that it does not catch errors or bad formatting...
For your situation, it looks like you'd want to add
{}
's around it (asjson_decode
also requires):which for me yields:
This regex will do the trick
This solution seems to be enough for most common purposes.