如何循环JSON并获取数据?

时间:2011-08-14 15:59:08

标签: javascript json parsing

您好我有足球比分的本地JSON文件。虽然使用JavaScript获得某个分数或团队名称,但我如何循环?

以下是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"
}

很抱歉,我还在学习,希望能够掌握这一点。

我会这样做

eval(JSONfile)??我不知道该怎么做才能解析并阅读它?

2 个答案:

答案 0 :(得分:1)

要解析它,您可以使用.eval(),如您在问题中所述。

最好是使用JSON解析器。现代浏览器内置它:

var js_obj = JSON.parse( my_json );

对于不支持JSON.parse的浏览器,您可以添加.parse.stringify添加的json2 library

然后就像通常那样枚举for-in语句(或数组的for语句)。

答案 1 :(得分:0)

编辑:抱歉没有告诉你如何阅读JSON字符串。

如果您将字符串直接写入JavaScipt,例如。使用PHP,您可以直接将其设置为变量:

var scores = <?php echo $scoresJSON ?>

如果它作为字符串存储在JavaScript中,那么如果支持则可以使用JSON.parse,否则为eval:

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

或者您可以使用jQuery的方法。

参考文献:
http://api.jquery.com/jQuery.parseJSON/
https://developer.mozilla.org/En/Using_native_JSON#Parsing_JSON.c2.a0strings

至于那时使用数据:

/**
 * 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;
}

我在浏览器(Chrome)中对此进行了测试:

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