简体   繁体   中英

Loop through json

I'm trying to loop through json with jQuery ajax. 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. But, now on the for loop in javascript I need $htmlvalue[i] for I can parse the data.

Thanks for the help. Hope I described this right.

PHP FILE:

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

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. the JSON you have supplied is an array of mixed objects, some strings, eg "eid" and some are objects eg {"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

You have to change a way of converting your XML data to JSON array. Now it's not in format which you trying to handle by your 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. Using JSON.parse shouldn't be required, because you've passed already information about response format in $.ajax call by using dataType: "JSON" .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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