简体   繁体   English

遍历json

[英]Loop through json

I'm trying to loop through json with jQuery ajax. 我正在尝试使用jQuery ajax遍历json。 For some reason it's not allowing me to do so. 由于某种原因,它不允许我这样做。 I have received the json from PHP and I JSON.stringify it because it was it was giving me back objects. 我已经从PHP收到了json,并且我JSON.stringify了它,因为它正在给我返回对象。 But, now on the for loop in javascript I need $htmlvalue[i] for I can parse the data. 但是,现在在javascript中的for循环上,我需要$ htmlvalue [i]来解析数据。

Thanks for the help. 谢谢您的帮助。 Hope I described this right. 希望我描述了这个权利。

PHP FILE: 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: JavaScript文件:

$.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");
      }
  });

Server Response: 服务器响应:

["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"}]

The json you are trying to parse is not in a format that is easily parsable into meangingful objects. 您尝试解析的json格式不容易解析为有意义的对象。 the JSON you have supplied is an array of mixed objects, some strings, eg "eid" and some are objects eg {"0": "2014091100"}. 您提供的JSON是一个混合对象数组,一些字符串,例如“ eid”,有些是对象,例如{“ 0”:“ 2014091100”}。 Your problem is that you are trying to fudge data into a format which is not really suitable. 您的问题是您试图将数据伪装成一种不太合适的格式。 The data is already formatted as it is XML, treat the data as XML and don't try and fudge it into JSON 数据已经按照XML格式进行了格式化,将数据视为XML,请勿尝试将其捏入JSON中

You have to change a way of converting your XML data to JSON array. 您必须更改将XML数据转换为JSON数组的方式。 Now it's not in format which you trying to handle by your JavaScript. 现在,它不是您尝试通过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);

Next step is removing JSON.stringify, because you want to read your response as array, not as string. 下一步是删除JSON.stringify,因为您希望将响应读取为数​​组而不是字符串。 Using JSON.parse shouldn't be required, because you've passed already information about response format in $.ajax call by using dataType: "JSON" . 不需要使用JSON.parse,因为您已经通过使用dataType: "JSON"$.ajax调用中传递了有关响应格式的信息。

If it still won't work check your response in console and provide results here. 如果仍然无法正常工作,请在控制台中检查您的响应并在此处提供结果。 It would be also to good idea to use $.each() instead of for() , beacuse it will be easier to read this array. 使用$.each()代替for()也是一个好主意,因为读取此数组会更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM