简体   繁体   中英

How to loop through JSON and grab data?

Hi I have a local JSON file for football scores. How would I loop though it get a certain score or team name using JavaScript?

Below is the JSON:

{
   "w": 1,
   "t": "PRE",
   "gms":    [
            {
         "d": "Thu",
         "gsis": 55425,
         "h": "NE",
         "vnn": "Jaguars",
         "q": "F",
         "vs": 12,
         "v": "JAC",
         "rz": -1,
         "hs": 47,
         "hnn": "Patriots",
         "t": "7:30",
         "ga": "",
         "eid": 2011081152
      },
            {
         "d": "Thu",
         "gsis": 55424,
         "h": "PHI",
         "vnn": "Ravens",
         "q": "F",
         "vs": 6,
         "v": "BAL",
         "rz": -1,
         "hs": 13,
         "hnn": "Eagles",
         "t": "7:30",
         "ga": "",
         "eid": 2011081151
      },
            {
         "d": "Thu",
         "gsis": 55423,
         "h": "SD",
         "vnn": "Seahawks",
         "q": "F",
         "vs": 24,
         "v": "SEA",
         "rz": -1,
         "hs": 17,
         "hnn": "Chargers",
         "t": "8:00",
         "ga": "",
         "eid": 2011081153
      },
            {
         "d": "Thu",
         "gsis": 55426,
         "h": "DAL",
         "vnn": "Broncos",
         "q": "F",
         "vs": 23,
         "v": "DEN",
         "rz": -1,
         "hs": 24,
         "hnn": "Cowboys",
         "t": "8:30",
         "ga": "",
         "eid": 2011081154
      },
            {
         "d": "Thu",
         "gsis": 55427,
         "h": "OAK",
         "vnn": "Cardinals",
         "q": "F",
         "vs": 24,
         "v": "ARI",
         "rz": -1,
         "hs": 18,
         "hnn": "Raiders",
         "t": "10:00",
         "ga": "",
         "eid": 2011081155
      },
            {
         "d": "Fri",
         "gsis": 55430,
         "h": "ATL",
         "vnn": "Dolphins",
         "q": "F",
         "vs": 28,
         "v": "MIA",
         "rz": -1,
         "hs": 23,
         "hnn": "Falcons",
         "t": "7:30",
         "ga": "",
         "eid": 2011081252
      },
            {
         "d": "Fri",
         "gsis": 55429,
         "h": "DET",
         "vnn": "Bengals",
         "q": "F",
         "vs": 3,
         "v": "CIN",
         "rz": -1,
         "hs": 34,
         "hnn": "Lions",
         "t": "7:30",
         "ga": "",
         "eid": 2011081251
      },
            {
         "d": "Fri",
         "gsis": 55431,
         "h": "WAS",
         "vnn": "Steelers",
         "q": "F",
         "vs": 7,
         "v": "PIT",
         "rz": -1,
         "hs": 16,
         "hnn": "Redskins",
         "t": "7:30",
         "ga": "",
         "eid": 2011081253
      },
            {
         "d": "Fri",
         "gsis": 55428,
         "h": "KC",
         "vnn": "Buccaneers",
         "q": "F",
         "vs": 25,
         "v": "TB",
         "rz": -1,
         "hs": 0,
         "hnn": "Chiefs",
         "t": "8:00",
         "ga": "",
         "eid": 2011081255
      },
            {
         "d": "Fri",
         "gsis": 55432,
         "h": "NO",
         "vnn": "49ers",
         "q": "F",
         "vs": 3,
         "v": "SF",
         "rz": -1,
         "hs": 24,
         "hnn": "Saints",
         "t": "8:00",
         "ga": "",
         "eid": 2011081254
      },
            {
         "d": "Sat",
         "gsis": 55433,
         "h": "CLE",
         "vnn": "Packers",
         "q": "F",
         "vs": 17,
         "v": "GB",
         "rz": -1,
         "hs": 27,
         "hnn": "Browns",
         "t": "7:30",
         "ga": "",
         "eid": 2011081351
      },
            {
         "d": "Sat",
         "gsis": 55437,
         "h": "CAR",
         "vnn": "Giants",
         "q": "F",
         "vs": 10,
         "v": "NYG",
         "rz": -1,
         "hs": 20,
         "hnn": "Panthers",
         "t": "8:00",
         "ga": "",
         "eid": 2011081355
      },
            {
         "d": "Sat",
         "gsis": 55434,
         "h": "CHI",
         "vnn": "Bills",
         "q": "F",
         "vs": 3,
         "v": "BUF",
         "rz": -1,
         "hs": 10,
         "hnn": "Bears",
         "t": "8:00",
         "ga": "",
         "eid": 2011081352
      },
            {
         "d": "Sat",
         "gsis": 55435,
         "h": "STL",
         "vnn": "Colts",
         "q": "F",
         "vs": 10,
         "v": "IND",
         "rz": -1,
         "hs": 33,
         "hnn": "Rams",
         "t": "8:00",
         "ga": "",
         "eid": 2011081353
      },
            {
         "d": "Sat",
         "gsis": 55436,
         "h": "TEN",
         "vnn": "Vikings",
         "q": "F",
         "vs": 3,
         "v": "MIN",
         "rz": -1,
         "hs": 14,
         "hnn": "Titans",
         "t": "8:00",
         "ga": "",
         "eid": 2011081354
      },
            {
         "d": "Mon",
         "gsis": 55438,
         "h": "HOU",
         "vnn": "Jets",
         "q": "P",
         "vs": 0,
         "v": "NYJ",
         "rz": -1,
         "hs": 0,
         "hnn": "Texans",
         "t": "8:00",
         "ga": "",
         "eid": 2011081551
      }
   ],
   "y": 2011,
   "gd": "0"
}

Sorry guys I am still learning and want to be able to master this.

So would I do

eval(JSONfile)?? I am not sure what to do here to parse and read this?

To parse it, you could use .eval() as you noted in your question.

Better is to use a JSON parser. Modern browsers have it built in:

var js_obj = JSON.parse( my_json );

For browsers that don't support JSON.parse , you can include the json2 library that will add .parse and .stringify .

Then just enumerate with a for-in statement (or a for statement for Arrays) like you normally would.

Edit: Sorry didn't tell you how to read in the JSON string.

If you are writing the string straight into the JavaScipt, Eg with PHP, you can just set it straight as a variable:

var scores = <?php echo $scoresJSON ?>

If it's stored in JavaScript as a string, then you can use JSON.parse if it's supported, otherwise eval:

var scores = {};
if(typeof(JSON) === 'object') { scores = JSON.parse(jsonString); }
else { scores = eval(jsonString); }

Or you can use jQuery's methods.

References:
http://api.jquery.com/jQuery.parseJSON/
https://developer.mozilla.org/En/Using_native_JSON#Parsing_JSON.c2.a0strings

As for then using the data:

/**
 * Return array of all scores for a team acronym
 * E.g. for MIN it would return: [3]
 * If the same team had multiple games it would return scores for all games.
 * E.g.: [3, 6, 0, 23]
 */
function getScoresForTeam(teamAcronym, scoresObject) {
    // Create scores array, to populate later
    var scores = [];

    // Look through each game in JSON
    for(i = 0; i < jsonString.gms.length; i++) {
        var thisGame = jsonString.gms[i]
        // Check if they were team 'v'
        if(thisGame.v === teamAcronym) {
            scores.push(thisGame.vs);
        } else if(thisGame.h === teamAcronym) {
            scores.push(thisGame.hs);
        }
    }

    // Return scores array
    return scores;
}

I tested this in browser (Chrome):

> getScoresForTeam('MIN',scores);
> [3]

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