Johan
Johan

Reputation: 35223

Json_decode returns nothing

I'm sending a stringified json object to a php page. I want to loop through it in php. When I use:

echo $_POST['Tags'];

that results in:

{\"0\":\"tag1\",\"1\":\"tag2\"}

but

echo json_decode($_POST['Tags'], true/false);

doesn't print anything. Shouldn't I at least get Array?

Upvotes: 1

Views: 1937

Answers (1)

TimWolla
TimWolla

Reputation: 32731

php > var_dump(json_decode('{\"0\":\"tag1\",\"1\":\"tag2\"}'));
NULL

You really should use var_dump if you don't get any visible output. And as the php.net documentation ( states:

Return Values

Returns the value encoded in json in appropriate PHP type. Values true, false and null (case-insensitive) are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

your JSON is invalid.
The correct JSON would be:

["tag1","tag2"]

or

{"0":"tag1","1":"tag2"}

Without the Backslashes.

Upvotes: 2

Related Questions