post JSON object to PHP

so i have a gigantic javascript object that i want to pass it on to PHP.

What i'm trying is: - use stringify to put it as the value of a hidden field - hit submit

In PHP, if I

echo $_POST['hidden'] 

the JSON string seems perfect, but when i use

json_decode($_POST['hidden']) 

i get NULL

If i use jQuery's

$.post

, i get exactly the desired result: i am able to use json_decode on it.

Can someone explain to me what i'm doing wrong? thanks

Upvotes: 1

Views: 1393

Answers (3)

shenhengbin
shenhengbin

Reputation: 4294

here is same error types

//wrong
$str1 = <<<EOD
{“dealList”:”\r\n\t”}
EOD;

//right
$str2 = <<<EOD
{“dealList”:”\\r\\n\\t”}
EOD;

//wrong
$str3 = <<<EOD
{‘dealList’:'\r\n’}
EOD;

//wrong
$str4 = <<<EOD
{‘dealList’:'\\r\\n’}
EOD;

//wrong
$str5 = “{‘dealList’:'\r\n’}”;

//wrong
$str6 = “{‘dealList’:'\\r\\n’}”;

//right
$str7 = ‘{“dealList”:”\r\n”}’;

$c = json_decode($str1);

Upvotes: 0

Eugene
Eugene

Reputation: 3375

Maybe you need to do urldecode? http://php.net/manual/en/function.urldecode.php

Upvotes: 1

Moe Sweet
Moe Sweet

Reputation: 3721

Your JSON string might contain some extra slashes. Try strip_slashes before json_decode.

Upvotes: 1

Related Questions