您好我是JSON的新手并遇到了一些问题。
我正在使用jquery的$ .getJSON方法请求一些信息,如下所示:
function getPlayerPositions(type) { //left/top/move
var loadUrl = "../php/client_communication/relay_positions.php";
var playerPos = [];
$.ajaxSetup ({
cache: false,
async: false
});
$.getJSON( loadUrl, { type: type } )
.done(function( data ) {
useReturnData(data);
})
.fail(function( jqxhr, textStatus, error ) {
var err = textStatus + ', ' + error;
console.log( "Request Failed: " + err);
});
function useReturnData(data){
playerPos = data;
alert("response: "+playerPos);
};
//reset to asynchronous ajax from now on
$.ajaxSetup ({
async: true
});
alert(playerPos[0]);
return playerPos;
}
当我的函数先前运行到这个时候,我收到正确的JSON编码数据,然后我可以通过我的“playerPos”数组访问。 (即警报(playerPos [4]))
但这次我收到的数据包含多个嵌套对象:
这是ajax响应警告:
响应:[{“位置”:“LB”,“ID”:“2”,“x-offset”:“0”,“y-offset”:“0”,“统计”:{“ID “:”2“,”IMG“:”/ images / player_avatars / player_test.png“,”First_Name“:”Daniel“,”Surname“:”Vernall“,”Nickname“:”Tall Tree“,”number“: “25”, “Fav_Pos_1”: “LB”, “Fav_Pos_2”: “CB”, “团队”: “A”, “SPEED”: “100”, “灵活性”: “100”, “实力”:“100 ”, “跳跃”: “100”, “标记”: “100”, “STAMINA”: “100”, “领导”: “100”, “适应性”: “100”, “RESTRAINT”: “100”, “INJURY_PRONE”: “100”, “HEAL_TIME”: “100”, “MORALE”: “100”, “攻击性”: “100”, “传递”: “100”, “SHOOTING_ACCURACY”: “100”,“SHOOTING_POWER “:” 100" , “标题”: “100”, “MISC_BODY”: “100”, “定位”: “100”, “FIRST_TOUCH”: “100”, “LONG_DISTANCE”: “100”, “STRONG_FOOT”: “0”, “对照”: “100”, “CURLING”: “100”, “凿”: “100”, “截击”: “100”, “SET_PIECES”: “100”, “THROW_INS”:“100 ”, “反射”: “100”, “ONE_ON_ONES”: “100”, “AERIAL_ABILITY”: “100”, “捉”: “100”, “协调”: “100”, “投掷”: “100”, “coordX”:0, “coordY”:0, “yellowCards”:0, “redCards”:0, “shotsOnTarget”:0, “shotsOffTarget”:0, “目标”:0, “助攻”:0,“合作mpletedPasses “:0,” incompletePasses “:0,” 滑车 “:0”,timesTackled “:0,” 截获 “:0,” 截获 “:0”,badReceive “:0}},{” 位置 “:” LCB “,”ID“:”3“,”x-offset“:”0“,”y-offset“:”0“,”Stats“:{”ID“:”3“,”IMG“:”/ images /player_avatars/player_test.png","First_Name":"Teddy","Surname":"Vernall","Nickname":"Bear","number":"11","Fav_Pos_1":"ST","Fav_Pos_2 “:” CAM”, “团队”: “A”, “SPEED”: “100”, “灵活性”: “100”, “实力”: “100”, “JUMP”: “100”, “标识”: “100”, “STAMINA”: “100”, “领导”: “100”, “适应性”: “100”, “REST ......” SET_PIECES “:” 100" , “THROW_INS”: “100” “反射”: “100”, “ONE_ON_ONES”: “100”, “AERIAL_ABILITY”: “100”, “捉”: “100”, “协调”: “100”, “投掷”: “100”,“coordX “:0,” coordY “:0,” yellowCards “:0,” redCards “:0,” shotsOnTarget “:0,” shotsOffTarget “:0,” 目标 “:0,” 助攻 “:0,” completedPasses“: 0 “incompletePasses”:0, “滑车”:0 “timesTackled”:0, “截获”:0, “截获”:0 “badReceive”:0}},{ “位置”: “GK”,” ID“:”12“,”x-offset“:”0“,”y-offset“:”0“,”Stats“:{”ID“:”12“,”IMG“:”/ images / player_avatars / player_test .png“,”First_Name“:”Rumple“,”Surname“:”Stiltskin“,”昵称“:”Rumpy Pump Stink“,”number“:”29“,”Fav_Pos_1“:”CDM“,”Fav_Pos_2“: “LB”, “团队”: “A”, “SPEED”: “100”, “灵活性”: “100”, “实力”: “100”, “JUMP”: “100”, “标识”:“100 ”, “STAMINA”: “100”, “领导”: “100”, “适应性”: “100”, “RESTRAINT”: “100”, “INJURY_PRONE”: “100”, “HEAL_TIME”: “100”, “MORALE”: “100”, “攻击性”: “100”, “传递”: “100”, “SHOOTING_ACCURACY”: “100”, “SHOOTING_POWER”: “100”, “标题”: “100”,“MISC_BODY “:” 100" , “定位”: “100”, “FIRST_TOUCH”: “100”, “LONG_DISTANCE”: “100”, “STRONG_FOOT”: “50”, “对照”: “100”, “CURLING”: “100”, “凿”: “100”, “截击”: “100”, “SET_PIECES”: “100”, “THROW_INS”: “100”, “反射”: “100”, “ONE_ON_ONES”:“100 ”, “AERIAL_ABILITY”: “100”, “捉”: “100”, “协调”: “100”, “投掷”: “100”, “coordX”:0 “coordY”:0 “yellowCards”: 0, “redCards”:0, “shotsOnTarget”:0, “shotsOffTarget”:0, “目标”:0, “助攻”:0, “completedPasses”:0, “incompletePasses”:0, “铲球”:0, “timesTackled”:0, “截获”:0, “截获”:0 “badReceive”:0}}]
Javascript似乎不将这些视为单个对象:
例如,在警告playerPos [4]时,它只会提醒上面字符串中的第4个字符。
我意识到上述内容非常难以阅读,但我想向您展示警报的实际副本。
这是它访问的php文件中的位:
if($type=="db_request"){
$team = new Team;
$team = $team->buildTeam(101, 'A');
ChromePhp::log($team);
$response = json_encode($team);
}
我上面的日志看起来像这样:
所以你可以看到php文件将“$ team”视为包含多个嵌套对象
有什么想法吗?
答案 0 :(得分:1)
好像你是对对象进行双重编码(类似于echo json_encode($response);
)。然后,jQuery会将JSON编码的字符串解析为JSON编码的对象字符串...
您可以通过在浏览器的网络检查器(Chrome devtools,Opera Dragonfly,Firebug)中检查有效的HTTP响应来检查此类服务器端故障。
答案 1 :(得分:0)
"Stats":{
"ID":"3",
"IMG":"/images/player_avatars/player_test.png",
"First_Name":"Teddy",
"Surname":"Vernall",
"Nickname":"Bear",
"number":"11",
"Fav_Pos_1":"ST",
"Fav_Pos_2":"CAM",
"team":"A",
"SPEED":"100",
"AGILITY":"100",
"STRENGTH":"100",
"JUMP":"100",
"MARKING":"100",
"STAMINA":"100",
"LEADERSHIP":"100",
"ADAPTABILITY":"100",
"REST...,"SET_PIECES":"100",
"THROW_INS":"100",
"REFLEXES":"100",
"ONE_ON_ONES":"100",
"AERIAL_ABILITY":"100",
"CATCHING":"100",
"COORDINATION":"100",
"THROWING":"100",
"coordX":0,
"coordY":0,
"yellowCards":0,
"redCards":0,
"shotsOnTarget":0,
"shotsOffTarget":0,
"goals":0,
"assists":0,
"completedPasses":0,
"incompletePasses":0,
"tackles":0,
"timesTackled":0,
"intercepts":0,
"intercepted":0,
"badReceive":0
}
请参阅SyntaxError: Unexpected token S
SET_PIECES
已经突破json
使其无效。
答案 2 :(得分:0)
我知道这已经得到了解答,但我注意到了一些可能有助于安静的性能提示。在可能的情况下避免嵌套功能确实可以帮助加速应用程序。
function getPlayerPositions(type) { //left/top/move
var loadUrl = "../php/client_communication/relay_positions.php";
var playerPos = [];
$.ajax( loadUrl, { type: type, async: false, cache: false, dataType: "json" } )
.done(function( data ) {
playerPos = data;
alert("response: "+playerPos);
})
.fail(function( jqxhr, textStatus, error ) {
var err = textStatus + ', ' + error;
console.log( "Request Failed: " + err);
});
alert(playerPos[0]);
return playerPos;
}
更多信息here