Reputation: 1089
I have JSON string like:
'"[{\"type\": \"EDITOR\", \"value\": \"fsddfsdsfdfs\"}, {\"type\": \"CITA\", \"value\": \"Bug:\\n\\t\\t\\t\\t\\t0 open / 0\\n\\t\\t\\t\\n \\n Feature:\\n\\t\\t\\t\\t\\t1 open / 1\\n\\t\\t\\t\"}]"'
json_decode fails to decode it.
Removing " mark from start and end makes it to work
'[{\"type\": \"EDITOR\", \"value\": \"fsddfsdsfdfs\"}, {\"type\": \"CITA\", \"value\": \"Bug:\\n\\t\\t\\t\\t\\t0 open / 0\\n\\t\\t\\t\\n \\n Feature:\\n\\t\\t\\t\\t\\t1 open / 1\\n\\t\\t\\t\"}]'
test code
$test1 = '"[{"type": "EDITOR", "value": "fsddfsdsfdfs"}, {"type": "CITA", "value": "Bug: 0 open / 0 Feature: 1 open / 1 "}]"';
print_r(json_decode($test1));
echo json_last_error().'<br>';
$test2 = '[{"type": "EDITOR", "value": "fsddfsdsfdfs"}, {"type": "CITA", "value": "Bug: 0 open / 0 Feature: 1 open / 1 "}]';
print_r(json_decode($test2));
Am I missing something or using it wrong?
Upvotes: 0
Views: 260
Reputation: 619
This appened to me too after moving from php5.2 to 5.3.
I have a script that send from javascript using JSON.stringify(response) and receive in PHP with json_decode, and it stopped working.
Don't know the differences from the two php versions, but i had to add stripslashes to make i work again.
$array=json_decode(stripslashes($IDS));
Upvotes: 0
Reputation: 9292
You should not call your string a "JSON string", because it is none. You surely did not get this string using json_encode(). The grammar for JSON has only two top-level productions:
JSON is either an object {...} or an array [...] (and nothing else)
It follows that:
JSON-string is either "{...}" or "[...]" or '{...}' or '[...]' (and nothing else)
Upvotes: 2
Reputation: 8078
The " mark at the start and end marks its content as a string, disabling parser to detect objects ({}) and arrays ([])
Remove it as you found and it will work.
Upvotes: 2