我正在尝试使用jQuery ajax循环遍历json。出于某种原因,它不允许我这样做。我收到了PHP的json和我JSON.stringify它,因为它是给我回来的对象。但是,现在在javascript的for循环中,我需要$ htmlvalue [i],因为我可以解析数据。
感谢您的帮助。 希望我说得对。
PHP文件:
<?php
$nfl = simplexml_load_file('http://www.nfl.com/liveupdate/scorestrip/ss.xml');
$items = array();
foreach ($nfl->gms->g as $game) {
foreach($game->attributes() as $a => $b) {
$items[] = $a;
$items[] = $b;
}
}
echo json_encode($items);
?>
Javascript FILE:
$.ajax({
type:"post",
url: "nfl.php",
dataType: "JSON",
success: function(response){
var $data = response,
$htmlvalue = '';
$htmlvalue = JSON.stringify($data.length);
for(i=0; i < $htmlvalue.length; i++){
console.log('<h4>' +$htmlvalue[i].eid + "</h4>");
}
},
error: function(){
console.log("error");
}
});
服务器响应:
["eid",{"0":"2014091100"},"gsis",{"0":"56186"},"d",{"0":"Thu"},"t",{"0":"8:25"},"q",{"0":"F"},"h",{"0":"BAL"},"hnn",{"0":"ravens"},"hs",{"0":"26"},"v",{"0":"PIT"},"vnn",{"0":"steelers"},"vs",{"0":"6"},"rz",{"0":"0"},"ga",{"0":""},"gt",{"0":"REG"},"eid",{"0":"2014091400"},"gsis",{"0":"56187"},"d",{"0":"Sun"},"t",{"0":"1:00"},"q",{"0":"F"},"h",{"0":"BUF"},"hnn",{"0":"bills"},"hs",{"0":"29"},"v",{"0":"MIA"},"vnn",{"0":"dolphins"},"vs",{"0":"10"},"rz",{"0":"0"},"ga",{"0":""},"gt",{"0":"REG"},"eid",{"0":"2014091401"},"gsis",{"0":"56188"},"d",{"0":"Sun"},"t",{"0":"1:00"},"q",{"0":"F"},"h",{"0":"CAR"},"hnn",{"0":"panthers"},"hs",{"0":"24"},"v",{"0":"DET"},"vnn",{"0":"lions"},"vs",{"0":"7"},"rz",{"0":"0"},"ga",{"0":""},"gt",{"0":"REG"},"eid",{"0":"2014091402"},"gsis",{"0":"56189"},"d",{"0":"Sun"},"t",{"0":"1:00"},"q",{"0":"F"},"h",{"0":"CIN"},"hnn",{"0":"bengals"},"hs",{"0":"24"},"v",{"0":"ATL"},"vnn",{"0":"falcons"},"vs",{"0":"10"},"rz",{"0":"0"},"ga",{"0":""},"gt",{"0":"REG"},"eid",{"0":"2014091403"},"gsis",{"0":"56190"},"d",{"0":"Sun"},"t",{"0":"1:00"},"q",{"0":"F"},"h",{"0":"CLE"},"hnn",{"0":"browns"},"hs",{"0":"26"},"v",{"0":"NO"},"vnn",{"0":"saints"},"vs",{"0":"24"},"rz",{"0":"0"},"ga",{"0":""},"gt",{"0":"REG"},"eid",{"0":"2014091404"},"gsis",{"0":"56191"},"d",{"0":"Sun"},"t",{"0":"1:00"},"q",{"0":"F"},"h",{"0":"MIN"},"hnn",{"0":"vikings"},"hs",{"0":"7"},"v",{"0":"NE"},"vnn",{"0":"patriots"},"vs",{"0":"30"},"rz",{"0":"0"},"ga",{"0":""},"gt",{"0":"REG"},"eid",{"0":"2014091405"},"gsis",{"0":"56192"},"d",{"0":"Sun"},"t",{"0":"1:00"},"q",{"0":"F"},"h",{"0":"NYG"},"hnn",{"0":"giants"},"hs",{"0":"14"},"v",{"0":"ARI"},"vnn",{"0":"cardinals"},"vs",{"0":"25"},"rz",{"0":"0"},"ga",{"0":""},"gt",{"0":"REG"},"eid",{"0":"2014091406"},"gsis",{"0":"56193"},"d",{"0":"Sun"},"t",{"0":"1:00"},"q",{"0":"F"},"h",{"0":"TEN"},"hnn",{"0":"titans"},"hs",{"0":"10"},"v",{"0":"DAL"},"vnn",{"0":"cowboys"},"vs",{"0":"26"},"rz",{"0":"0"},"ga",{"0":""},"gt",{"0":"REG"},"eid",{"0":"2014091407"},"gsis",{"0":"56194"},"d",{"0":"Sun"},"t",{"0":"1:00"},"q",{"0":"F"},"h",{"0":"WAS"},"hnn",{"0":"redskins"},"hs",{"0":"41"},"v",{"0":"JAC"},"vnn",{"0":"jaguars"},"vs",{"0":"10"},"rz",{"0":"0"},"ga",{"0":""},"gt",{"0":"REG"},"eid",{"0":"2014091408"},"gsis",{"0":"56195"},"d",{"0":"Sun"},"t",{"0":"4:05"},"q",{"0":"F"},"h",{"0":"SD"},"hnn",{"0":"chargers"},"hs",{"0":"30"},"v",{"0":"SEA"},"vnn",{"0":"seahawks"},"vs",{"0":"21"},"rz",{"0":"0"},"ga",{"0":""},"gt",{"0":"REG"},"eid",{"0":"2014091409"},"gsis",{"0":"56196"},"d",{"0":"Sun"},"t",{"0":"4:05"},"q",{"0":"F"},"h",{"0":"TB"},"hnn",{"0":"buccaneers"},"hs",{"0":"17"},"v",{"0":"STL"},"vnn",{"0":"rams"},"vs",{"0":"19"},"rz",{"0":"0"},"ga",{"0":""},"gt",{"0":"REG"},"eid",{"0":"2014091410"},"gsis",{"0":"56197"},"d",{"0":"Sun"},"t",{"0":"4:25"},"q",{"0":"F"},"h",{"0":"DEN"},"hnn",{"0":"broncos"},"hs",{"0":"24"},"v",{"0":"KC"},"vnn",{"0":"chiefs"},"vs",{"0":"17"},"rz",{"0":"0"},"ga",{"0":""},"gt",{"0":"REG"},"eid",{"0":"2014091411"},"gsis",{"0":"56198"},"d",{"0":"Sun"},"t",{"0":"4:25"},"q",{"0":"F"},"h",{"0":"GB"},"hnn",{"0":"packers"},"hs",{"0":"31"},"v",{"0":"NYJ"},"vnn",{"0":"jets"},"vs",{"0":"24"},"rz",{"0":"0"},"ga",{"0":""},"gt",{"0":"REG"},"eid",{"0":"2014091412"},"gsis",{"0":"56199"},"d",{"0":"Sun"},"t",{"0":"4:25"},"q",{"0":"F"},"h",{"0":"OAK"},"hnn",{"0":"raiders"},"hs",{"0":"14"},"v",{"0":"HOU"},"vnn",{"0":"texans"},"vs",{"0":"30"},"rz",{"0":"0"},"ga",{"0":""},"gt",{"0":"REG"},"eid",{"0":"2014091413"},"gsis",{"0":"56200"},"d",{"0":"Sun"},"t",{"0":"8:30"},"q",{"0":"F"},"h",{"0":"SF"},"hnn",{"0":"49ers"},"hs",{"0":"20"},"v",{"0":"CHI"},"vnn",{"0":"bears"},"vs",{"0":"28"},"rz",{"0":"0"},"ga",{"0":""},"gt",{"0":"REG"},"eid",{"0":"2014091500"},"gsis",{"0":"56201"},"d",{"0":"Mon"},"t",{"0":"8:30"},"q",{"0":"P"},"h",{"0":"IND"},"hnn",{"0":"colts"},"hs",{"0":"0"},"v",{"0":"PHI"},"vnn",{"0":"eagles"},"vs",{"0":"0"},"rz",{"0":"0"},"ga",{"0":""},"gt",{"0":"REG"}]
答案 0 :(得分:0)
您尝试解析的json不是一种易于解析为混合对象的格式。你提供的JSON是一个混合对象的数组,一些字符串,例如“eid”,有些是对象,例如{“0”:“2014091100”}。您的问题是您试图将数据转换为不合适的格式。数据已经格式化为XML,将数据视为XML,不要尝试将其融入JSON
答案 1 :(得分:0)
您必须更改将XML数据转换为JSON数组的方法。现在它不是你试图通过JavaScript处理的格式。
$items = array();
foreach ($nfl->gms->g as $game) {
$row = array();
foreach ($game->attributes() as $a => $b) {
$row[$a] = (string) $b;
}
$items[] = $row;
}
echo json_encode($items);
下一步是删除JSON.stringify,因为您希望将响应读作数组,而不是字符串。不应使用JSON.parse,因为您已使用$.ajax
在dataType: "JSON"
调用中传递了有关响应格式的信息。
如果仍然无效,请在控制台中检查您的回复并在此处提供结果。使用$.each()
代替for()
也是个好主意,因为读取此数组会更容易。