0
votes

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.

3
Are you sure this is all your input? Seem to be working fine for me.complex857
Printing $_POST['data'] gives me > {"user":"Mark","id":"80","0":["age","!=","twenty four","varchar"],"1":["prefix","=","Mr.","enum"]}HardCode
While var_dump(json_decode($_POST['data'])); gives me nullHardCode

3 Answers

0
votes

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

0
votes

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.

0
votes

You have to store it to something.

$posted = json_decode($_POST['data']);
var_dump($posted);