When I test this piece of code:
echo json_encode(array( 'foo' => 'bar')); die;
I get:
{"foo":"bar"}
What is valid JSON.
(Note that these are double quotes, not the simple quotes you sent)
You are asking:
{ foo : 'bar' }
valid Javascript but invalid JSON - so json_encode will not return this.
See json.org for a specification of the JSON format - which is a subset of Javascript and not Javascript itself.
Instead of “stripping yourself of quotes with some ugly regular expression”, you should adapt your code, so it accepts valid JSON: this is, in my opinion, better.
Pascal martin
source share