您好,我是 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","Stats":{"ID":" 2","IMG":"/images/player_avatars/player_test.png","First_Name":"Daniel","Surname":"Vernall","昵称":"Tall Tree","number":"25" ,"Fav_Pos_1":"LB","Fav_Pos_2":"CB","team":"A","SPEED":"100","AGILITY":"100","STRENGTH":"100","跳跃":"100","标记":"100","耐力":"100","领导力":"100","适应性":"100","约束":"100","INJURY_PRONE":"100","HEAL_TIME":"100","MORALE":"100","AGGRESSIVENESS":"100","PASSING":"100","SHOOTING_ACCURACY":"100","SHOOTING_POWER ":"100","HEADING":"100","MISC_BODY":"100","POSITIONING":"100","FIRST_TOUCH":"100","LONG_DISTANCE":"100","STRONG_FOOT": "0","CONTROL":"100","CURLING":"100","CHIPPING":"100","VOLLEYING":"100","SET_PIECES":"100","THROW_INS":"100 ","REFLEXES":"100","ONE_ON_ONES":"100","AERIAL_ABILITY":"100","捕捉":"100","协调":"100","投掷":"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}},{"Position":"LCB","ID":"3","x-offset":"0","y-offset":" 0","Stats":{"ID":"3","IMG":"/images/player_avatars/player_test.png","First_Name":"Teddy","Surname":"Vernall","昵称" :"熊","数字":"11","Fav_Pos_1":"ST","Fav_Pos_2":"CAM","team":"A","SPEED":"100","AGILITY":"100" “力量”:“100”,“跳跃”:“100”,“标记”:“100”,“耐力”:“100”,“领导力”:“100”,“适应性”:“100”,休息...,"SET_PIECES":"100","THROW_INS":"100","REFLEXES":"100","ONE_ON_ONES":"100","AERIAL_ABILITY":"100","CATCHING":" 100","协调":"100","投掷":"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}},{"Position":"GK","ID":"12","x-offset":"0","y-offset" :"0","Stats":{"ID":"12","IMG":"/images/player_avatars/player_test.png","First_Name":"Rumple","Surname":"Stiltskin"," Nickname":"Rumpy Pump Stink","number":"29","Fav_Pos_1":"CDM","Fav_Pos_2":"LB","team":"A","SPEED":"100","敏捷度:“100”,“力量”:“100”,“跳跃”:“100”,“标记”:“100”,“耐力”:“100”,“领导力”:“100”,“适应性” :"100","RESTRAINT":"100","INJURY_PRONE":"100","HEAL_TIME":"100","士气":"100","AGGRESSIVENESS":"100","PASSING":" 100","SHOOTING_ACCURACY":"100","SHOOTING_POWER":"100","HEADING":"100","MISC_BODY":"100","POSITIONING":"100","FIRST_TOUCH":"100" ,"LONG_DISTANCE":"100","STRONG_FOOT":"50","CONTROL":"100","CURLING":"100","CHIPPING":"100","VOLLEYING":"100","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}}]VOLLEYING":"100","SET_PIECES":"100","THROW_INS":"100","REFLEXES":"100","ONE_ON_ONES":"100","AERIAL_ABILITY":"100","CATCHING" :"100","协调":"100","投掷":"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,"截获":0,"badReceive":0}}]VOLLEYING":"100","SET_PIECES":"100","THROW_INS":"100","REFLEXES":"100","ONE_ON_ONES":"100","AERIAL_ABILITY":"100","CATCHING" :"100","协调":"100","投掷":"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,"截获":0,"badReceive":0}}]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}}]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}}]"redCards":0,"shotsOnTarget":0,"shotsOffTarget":0,"goals":0,"assists":0,"completedPasses":0,"incompletePasses":0,"tackles":0,"timesTackled ":0,"intercepts":0,"intercepted":0,"badReceive":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}}]
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”视为包含多个嵌套对象
有任何想法吗?