json_decode problem
Posted: Thu Jun 18, 2009 5:22 am
I am posting a JavaScript object using the prototype XMLHttpRequest class, but on the server after using json_decode one of my child arrays is not decoded - it remains in the string format;
--JScript
var params = "userid=" + userid + "&game=" + JSON.stringify(currentEditGame);
var ajaxUrl = 'http://www.mysite.com/Ajax.php';
new Ajax.Request(ajaxUrl, {
method:'post',
parameters:params,
onSuccess: function(transport){
var data = transport.responseText.evalJSON();
});
--PHP
$rawgame = stripslashes ($_POST['game']);
$game = json_decode($rawgame, true);
echo print_r($game);
--Output
Array
(
[game_id] => -1
[description] =>
[category] =>
[choices] => [{"choice_id": "-1", "media1": {"ID": "-1", "desc": "", "url": ""}, "media2": {"ID"
: "-1", "desc": "", "url": ""}}]
)
So I cannot access the choices array, I would have expected the Json_decode function to output;
Array
(
[game_id] => -1
[description] =>
[category] =>
[choices] => Array(
[game_id] => -1
[media1]=> Array(
[ID]=> -1
[desc]=>
[url]=>
)
[media2]=> Array(
[ID]=> -1
[desc]=>
[url]=>
)
)
)
From firebug the posted value is;
{"game_id":"-1","description":"","category":"","choices":"[{\"choice_id\": \"-1\", \"media1\": {\"ID
\": \"-1\", \"desc\": \"\", \"url\": \"\"}, \"media2\": {\"ID\": \"-1\", \"desc\": \"\", \"url\": \"
\"}}]"}
Any help greatly appreciated
Russ
--JScript
var params = "userid=" + userid + "&game=" + JSON.stringify(currentEditGame);
var ajaxUrl = 'http://www.mysite.com/Ajax.php';
new Ajax.Request(ajaxUrl, {
method:'post',
parameters:params,
onSuccess: function(transport){
var data = transport.responseText.evalJSON();
});
--PHP
$rawgame = stripslashes ($_POST['game']);
$game = json_decode($rawgame, true);
echo print_r($game);
--Output
Array
(
[game_id] => -1
[description] =>
[category] =>
[choices] => [{"choice_id": "-1", "media1": {"ID": "-1", "desc": "", "url": ""}, "media2": {"ID"
: "-1", "desc": "", "url": ""}}]
)
So I cannot access the choices array, I would have expected the Json_decode function to output;
Array
(
[game_id] => -1
[description] =>
[category] =>
[choices] => Array(
[game_id] => -1
[media1]=> Array(
[ID]=> -1
[desc]=>
[url]=>
)
[media2]=> Array(
[ID]=> -1
[desc]=>
[url]=>
)
)
)
From firebug the posted value is;
{"game_id":"-1","description":"","category":"","choices":"[{\"choice_id\": \"-1\", \"media1\": {\"ID
\": \"-1\", \"desc\": \"\", \"url\": \"\"}, \"media2\": {\"ID\": \"-1\", \"desc\": \"\", \"url\": \"
\"}}]"}
Any help greatly appreciated
Russ