无法在Javascript中解析JSON

时间:2013-02-21 01:39:51

标签: javascript json

我的HTML中有这个脚本

$(document).ready(function() { 
    type :'GET',
    url :'MapleLeafs2011.json',
    dataType :'json',
    success :processTeam,
    error :function() {
        alert('error');
    }
});

function processTeam(data) {
    var team = data.name;
    ("#team").html(team);
}

我试图获得以下JSON,

{
    "name": "Toronto Maple Leafs",
    "season": "2011-2012",
    "players": {
      "player": [
        {
          "age": "29",
          "height": "6-2",
          "number": "9",
          "name": "Colby Armstrong",
          "position": "RW",
          "weight": "195"
        },
        {
          "age": "20",
          "height": "6-3",
          "number": "37",
          "name": "Carter Ashton",
          "position": "RW",
          "weight": "215"
        },
        {
          "age": "22",
          "height": "6-5",
          "number": "59",
          "name": "Keith Aulie",
          "position": "D",
          "weight": "217"
        },
        {
          "age": "27",
          "height": "6-0",
          "number": "47",
          "name": "Darryl Boyce",
          "position": "C",
          "weight": "200"
        },
        {
          "age": "25",
          "height": "6-1",
          "number": "42",
          "name": "Tyler Bozak",
          "position": "C",
          "weight": "195"
        },
        {
          "age": "26",
          "height": "5-11",
          "number": "18",
          "name": "Mike Brown",
          "position": "RW",
          "weight": "205"
        },
        {
          "age": "22",
          "height": "6-5",
          "number": "32",
          "name": "Joe Colborne",
          "position": "C",
          "weight": "213"
        },
        {
          "age": "30",
          "height": "6-1",
          "number": "12",
          "name": "Tim Connolly",
          "position": "C",
          "weight": "190"
        },
        {
          "age": "28",
          "height": "6-1",
          "number": "46",
          "name": "Joey Crabb",
          "position": "RW",
          "weight": "190"
        },
        {
          "age": "26",
          "height": "6-0",
          "number": "11",
          "name": "Philippe Dupuis",
          "position": "C",
          "weight": "196"
        },
        {
          "age": "24",
          "height": "6-5",
          "number": "4",
          "name": "Cody Franson",
          "position": "D",
          "weight": "213"
        },
        {
          "age": "24",
          "height": "6-0",
          "number": "39",
          "name": "Matt Frattin",
          "position": "RW",
          "weight": "200"
        },
        {
          "age": "21",
          "height": "6-1",
          "number": "51",
          "name": "Jake Gardiner",
          "position": "D",
          "weight": "173"
        },
        {
          "age": "28",
          "height": "5-11",
          "number": "84",
          "name": "Mikhail Grabovski",
          "position": "C",
          "weight": "183"
        },
        {
          "age": "25",
          "height": "6-2",
          "number": "36",
          "name": "Carl Gunnarsson",
          "position": "D",
          "weight": "196"
        },
        {
          "age": "27",
          "height": "6-3",
          "number": "50",
          "name": "Jonas Gustavsson",
          "position": "G",
          "weight": "192"
        },
        {
          "age": "26",
          "height": "6-2",
          "number": "48",
          "name": "Ryan Hamilton",
          "position": "LW",
          "weight": "219"
        },
        {
          "age": "21",
          "height": "6-0",
          "number": "43",
          "name": "Nazem Kadri",
          "position": "C",
          "weight": "188"
        },
        {
          "age": "24",
          "height": "6-0",
          "number": "81",
          "name": "Phil Kessel",
          "position": "RW",
          "weight": "202"
        },
        {
          "age": "30",
          "height": "6-4",
          "number": "8",
          "name": "Mike Komisarek",
          "position": "D",
          "weight": "243"
        },
        {
          "age": "25",
          "height": "6-1",
          "number": "41",
          "name": "Nikolai Kulemin",
          "position": "LW",
          "weight": "225"
        },
        {
          "age": "31",
          "height": "5-10",
          "number": "24",
          "name": "John-Michael Liles",
          "position": "D",
          "weight": "185"
        },
        {
          "age": "29",
          "height": "5-11",
          "number": "15",
          "name": "Matthew Lombardi",
          "position": "C",
          "weight": "195"
        },
        {
          "age": "28",
          "height": "6-1",
          "number": "19",
          "name": "Joffrey Lupul",
          "position": "RW",
          "weight": "206"
        },
        {
          "age": "26",
          "height": "6-0",
          "number": "16",
          "name": "Clarke MacArthur",
          "position": "LW",
          "weight": "191"
        },
        {
          "age": "29",
          "height": "6-3",
          "number": "28",
          "name": "Colton Orr",
          "position": "RW",
          "weight": "222"
        },
        {
          "age": "26",
          "height": "6-3",
          "number": "3",
          "name": "Dion Phaneuf",
          "position": "D",
          "weight": "214"
        },
        {
          "age": "23",
          "height": "6-2",
          "number": "34",
          "name": "James Reimer",
          "position": "G",
          "weight": "220"
        },
        {
          "age": "26",
          "height": "6-3",
          "number": "38",
          "name": "Jay Rosehill",
          "position": "LW",
          "weight": "215"
        },
        {
          "age": "24",
          "height": "6-5",
          "number": "40",
          "name": "Jussi Rynnas",
          "position": "G",
          "weight": "205"
        },
        {
          "age": "22",
          "height": "6-2",
          "number": "2",
          "name": "Luke Schenn",
          "position": "D",
          "weight": "229"
        },
        {
          "age": "25",
          "height": "6-2",
          "number": "30",
          "name": "Ben Scrivens",
          "position": "G",
          "weight": "192"
        },
        {
          "age": "29",
          "height": "6-5",
          "number": "20",
          "name": "David Steckel",
          "position": "C",
          "weight": "215"
        }
      ]
  }
}

但Firefox说,此行上的 SyntaxError:无效标签url: "MapleLeafs2011.json",

Chrome在同一行上说未捕获的SyntaxError:意外的令牌:

可能出现什么问题?

2 个答案:

答案 0 :(得分:2)

ready()中函数的语法缺少对$ .get()或$ .ajax()的调用;

function() { 
    $.ajax({     
          type: 'GET',
          url: 'MapleLeafs2011.json',
          dataType: 'json',
          success: processTeam,
          error: function() {
             alert('error');
          }
    });
}

答案 1 :(得分:0)

你错过了

$.ajax({

线。您的对象缺少开头{,您需要将其传递给ajax函数,是吗?