对于我的问题,我再次感到抱歉,但我无法解决这个小问题。是新手的影响:)
我有这个JSON格式,表示每轮回合和每轮玩家的游戏列表。在该示例中仅代表1场比赛和3轮:
{
"1":
{
"4":
{
"Name":"Rocky",
"Time":"00:00:23"
},
"11":
{
"Name":"Rudy",
"Time":"00:00:36"
},
"25":
{
"Name":"Frank",
"Time":"00:00:04"
}
}
}
我希望它是一个正确的JSON格式:D
现在我将它发送到我的PHP文件并等待答案:
var DataJSON = JSON.stringify(Data);//Data is the General array
$.ajax({
data: { game: DataJSON},
url: 'page.php',
type: 'GET',
contentType: "application/json; charset=utf-8",
dataType: 'json',
beforeSend: function (){
alert('Information wird gespeichert');
console.log(DataJSON);
},
success: function (msg) {
alert('Information wurde gespeichert');
console.log("Succes" +msg);
},
error: function(msg){
alert('Server antwortet nicht. Fehler..');
console.log("Error here" +msg);
}
});
我的PHP文件的代码如下所示:
header('Content-Type: application/json');
if(isset($_GET['game'])) {
$ga= json_decode($_GET["game"],true);
foreach ( $ga as $ga){
echo $_GET['callback'] . '(' .(json_encode($ga[0])). ')';
}
}
else{
echo $_GET['callback'] . '(' .(json_encode("not found")). ')';
}
但控制台的答案是: “这里错误[object Object]”
我做错了什么,请帮助!!
答案 0 :(得分:0)
使用phpfiddle.org测试:
<?php
$_G = array(
"game" => '{
"1":
{
"4":
{
"Name":"Rocky",
"Time":"00:00:23"
},
"11":
{
"Name":"Rudy",
"Time":"00:00:36"
},
"25":
{
"Name":"Frank",
"Time":"00:00:04"
}
}
}',
"callback" => "callback");
if(isset($_G['game'])) {
$ga= json_decode($_G["game"],true);
echo "<pre>";
var_export($ga);
echo "</pre>";
foreach ( $ga as $ga){
echo $_G['callback'] . '(' .(json_encode($ga)). ')';
}
}
else{
echo $_G['callback'] . '(' .(json_encode("not found")). ')';
}
?>
当您致电$ga[0]
时,这会产生错误:
E_NOTICE:类型8 - 未定义的偏移量:0 - 第33行
由于您的数据中没有0索引,因此失败。我在上面的代码中删除了索引并正确执行。目前尚不清楚您想对数据做什么,所以如果这对您不起作用,请进行评论并进一步解释您的代码。
以下是我得到的结果:
array (
1 =>
array (
4 =>
array (
'Name' => 'Rocky',
'Time' => '00:00:23',
),
11 =>
array (
'Name' => 'Rudy',
'Time' => '00:00:36',
),
25 =>
array (
'Name' => 'Frank',
'Time' => '00:00:04',
),
),
)
callback({"4":{"Name":"Rocky","Time":"00:00:23"},"11":{"Name":"Rudy","Time":"00:00:36"},"25":{"Name":"Frank","Time":"00:00:04"}})