Reputation: 1633
i have a JSON data in Javascript made form array using JSON.stringify
{
"user":"Mark",
"id":"80",
"0":["age","=","twenty four","varchar"],
"1":["prefix","=","Mr.","enum"]
}
i am sending this via AJAX to PHP file. When i echo the POST i get the values
echo (serialize($_POST['data']));
s:263:
"{
"user":"Mark",
"id":"80",
"0":["age","=","twenty four","varchar"],
"1":["prefix","=","Mr.","enum"]
}";
How can i get the POSTed data in an Array or Object. i have tried to do
var_dump(json_decode($_POST['data']));
AND
var_dump(json_decode(serialize($_POST['data']))); AND var_dump(json_decode($_POST['data'],true));
but they did not work. Output is null.
Upvotes: 0
Views: 225
Reputation: 3959
You have to store it to something.
$posted = json_decode($_POST['data']);
var_dump($posted);
Upvotes: 0
Reputation: 42440
json_decode() should do the trick for you, but depending on your server config (if magic_quotes_gpc is on), you might need to use stripslashes() before decoding.
Upvotes: 0
Reputation: 10417
If your PHP versopn >=5.2.0, you can use following build in PHP functions to decode JSONS
json_decode($_POST['data'])
It return Array and StdClass object.
Edit: How did you found json_decode is not working. Please try ver_dump or print_r. Hoping your PHP version >=5.2.0
Upvotes: 0