需要帮助理解为什么我需要使用JSON.parse()

时间:2015-05-18 13:07:54

标签: javascript ajax json

我有一份当前网络开发课程的作业,我的问题是我无法理解为什么需要使用JSON.parse()(这是一项要求)。我可以猜到它将被使用的地方,但实验并未设法呈现任何结果。

一般的想法是我们将JSON数组中的信息绘制在一个单独的文件中(显然在同一台服务器上)。这是通过讲师写的ajax.js文件中包含的函数完成的,该文件相对简单如下:

function getJSON(url, callbackFunction) {

//var request = new XMLHttpRequest();
var request = new XMLHttpRequest();

// Work around for browser caching problem that affects some browsers. It prevents
// the browser from using a cached copy that was stored by the browser from another visit.

if (url.indexOf('?') < 0) {
    today = new Date();// Add a unique query string to the end of the url 
                       // representing the current time to force the browser to fetch the data
                       // rather than using a locally cached copy that the browser fetched some earlier time. 
    url += '?' + today.getTime();
}

request.onreadystatechange = function (){

    if (request.readyState === 4 && request.status===200) {
        alert("Response Status = " + request.status); // You can safely comment out this line 
        alert("response = " + request.response);      // You can safely comment out this line 
        alert("response headers =  " + request.getAllResponseHeaders()); // You can safely comment out this line
        callbackFunction(request.response);
    }
};

try {
    request.open("POST", url);
    request.responseType ="json";
    request.send();
} 
catch ( exception ) {
    alert(exception);
}

}

它使用XMLHTTPRequest函数。

这种情况下的回调函数目前如下:

function displayAllGames(results) {
    alert('ready to start work');

    var gamesArray = results.games;
    var myHTML = '<table id="table1" border="1">';

    for (var counter = 0; counter < gamesArray.length; counter++) {
        var game = gamesArray[counter];
        var venue = game.venue;
        var round = game.round;
        var date = new Date(game.date);
        var homeTeam = game.homeTeam.name;
        var awayTeam = game.awayTeam.name;

        myHTML += '<tr>';
        myHTML += '<td>' + round + '</td>';
        myHTML += '<td>' + venue + '</td>';
        myHTML += '<td>' + date.toLocaleDateString() + '</td>';
        myHTML += '<td>' + homeTeam + '</td>';
        myHTML += '<td>' + awayTeam + '</td>';
        myHTML += '</tr>';
    }

    myHTML += '</table>';

    var targetElement = document.getElementById('happyPlace');
    targetElement.innerHTML = myHTML;
}

在为JSON.parse()添加代码时,即使作为文档末尾的额外内容,也没有任何反应。代码如下:

var parsedData = JSON.parse(gamesArray);
alert(parsedData[1]);

但是没有警报出现,甚至在尝试使用document.write将单个项目写入文档末尾时也没有出现任何警报。有人可以解释一下我做错了什么吗?这让我疯了。

JSON内容:

{
"games": [
    {
        "round": 1,
        "date": "Fri, 3 Apr 2015 14:20:00",
        "venue": "Maryborough",
        "homeTeam":
                {
                    "name": "Maryborough",
                    "goals": 11,
                    "points": 9
                },
        "awayTeam":
                {
                    "name": "Castlemaine",
                    "goals": 24,
                    "points": 10
                }
    },
    {
        "round": 1,
        "date": "Fri, 3 Apr 2015 14:20:00",
        "venue": "Eaglehawk",
        "homeTeam":
                {
                    "name": "Eaglehawk",
                    "goals": 18,
                    "points": 19
                },
        "awayTeam":
                {
                    "name": "South Bendigo",
                    "goals": 3,
                    "points": 7
                }
    },
    {
        "round": 1,
        "date": "Fri, 3 Apr 2015 14:20:00",
        "venue": "Gisborne",
        "homeTeam":
                {
                    "name": "Gisborne",
                    "goals": 10,
                    "points": 5
                },
        "awayTeam":
                {
                    "name": "Kyneton",
                    "goals": 19,
                    "points": 11
                }
    },
    {
        "round": 1,
        "date": "Fri, 3 Apr 2015 14:20:00",
        "venue": "Golden Square",
        "homeTeam":
                {
                    "name": "Golden Square",
                    "goals": 16,
                    "points": 10
                },
        "awayTeam":
                {
                    "name": "Kangaroo Flat",
                    "goals": 9,
                    "points": 10
                }
    },
    {
        "round": 1,
        "date": "Fri, 3 Apr 2015 17:30:00",
        "venue": "Queen Elizabeth Oval",
        "homeTeam":
                {
                    "name": "Sandhurst",
                    "goals": 10,
                    "points": 9
                },
        "awayTeam":
                {
                    "name": "Strathfieldsaye",
                    "goals": 11,
                    "points": 16
                }
    },
    {
        "round": 2,
        "date": "Sat, 11 Apr 2015 14:20:00",
        "venue": "Gisborne",
        "homeTeam":
                {
                    "name": "Gisborne",
                    "goals": 25,
                    "points": 22
                },
        "awayTeam":
                {
                    "name": "Maryborough",
                    "goals": 7,
                    "points": 3
                }
    },
    {
        "round": 2,
        "date": "Sat, 11 Apr 2015 14:20:00",
        "venue": "Kennington",
        "homeTeam":
                {
                    "name": "South Bendigo",
                    "goals": 10,
                    "points": 6
                },
        "awayTeam":
                {
                    "name": "Golden Square",
                    "goals": 15,
                    "points": 18
                }
    },
    {
        "round": 2,
        "date": "Sat, 11 Apr 2015 14:20:00",
        "venue": "Kyneton",
        "homeTeam":
                {
                    "name": "Kyneton",
                    "goals": 15,
                    "points": 11
                },
        "awayTeam":
                {
                    "name": "Sandhurst",
                    "goals": 22,
                    "points": 30
                }
    },
    {
        "round": 2,
        "date": "Sat, 11 Apr 2015 17:30:00",
        "venue": "Kangaroo Flat",
        "homeTeam":
                {
                    "name": "Kangaroo Flat",
                    "goals": 12,
                    "points": 12
                },
        "awayTeam":
                {
                    "name": "Castlemaine",
                    "goals": 11,
                    "points": 10
                }
    },
    {
        "round": 2,
        "date": "Sun, 12 Apr 2015 14:20:00",
        "venue": "Strathfieldsaye",
        "homeTeam":
                {
                    "name": "Strathfieldsaye",
                    "goals": 16,
                    "points": 16
                },
        "awayTeam":
                {
                    "name": "Eaglehawk",
                    "goals": 11,
                    "points": 7
                }
    },
    {
        "round": 3,
        "date": "Sat, 18 Apr 2015 14:20:00",
        "venue": "Maryborough",
        "homeTeam":
                {
                    "name": "Maryborough",
                    "goals": 12,
                    "points": 14
                },
        "awayTeam":
                {
                    "name": "Kangaroo Flat",
                    "goals": 14,
                    "points": 13
                }
    },
    {
        "round": 3,
        "date": "Sat, 18 Apr 2015 14:20:00",
        "venue": "Castlemaine",
        "homeTeam":
                {
                    "name": "Castlemaine",
                    "goals": 12,
                    "points": 13
                },
        "awayTeam":
                {
                    "name": "Gisborne",
                    "goals": 10,
                    "points": 10
                }
    },
    {
        "round": 3,
        "date": "Sat, 18 Apr 2015 14:20:00",
        "venue": "Eaglehawk",
        "homeTeam":
                {
                    "name": "Eaglehawk",
                    "goals": 20,
                    "points": 21
                },
        "awayTeam":
                {
                    "name": "Kyneton",
                    "goals": 10,
                    "points": 8
                }
    },
    {
        "round": 3,
        "date": "Sat, 18 Apr 2015 14:20:00",
        "venue": "Golden Square",
        "homeTeam":
                {
                    "name": "Golden Square",
                    "goals": 6,
                    "points": 8
                },
        "awayTeam":
                {
                    "name": "Strathfieldsaye",
                    "goals": 13,
                    "points": 13
                }
    },
    {
        "round": 3,
        "date": "Sat, 18 Apr 2015 14:20:00",
        "venue": "Queen Elizabeth Oval",
        "homeTeam":
                {
                    "name": "Sandhurst",
                    "goals": 20,
                    "points": 17
                },
        "awayTeam":
                {
                    "name": "South Bendigo",
                    "goals": 8,
                    "points": 3
                }
    },
    {
        "round": 4,
        "date": "Sat, 25 Apr 2015 14:20:00",
        "venue": "Maryborough",
        "homeTeam":
                {
                    "name": "Maryborough",
                    "goals": 9,
                    "points": 4
                },
        "awayTeam":
                {
                    "name": "Sandhurst",
                    "goals": 25,
                    "points": 26
                }
    },
    {
        "round": 4,
        "date": "Sat, 25 Apr 2015 14:20:00",
        "venue": "Eaglehawk",
        "homeTeam":
                {
                    "name": "Eaglehawk",
                    "goals": 26,
                    "points": 14
                },
        "awayTeam":
                {
                    "name": "Kangaroo Flat",
                    "goals": 7,
                    "points": 3
                }
    },
    {
        "round": 4,
        "date": "Sat, 25 Apr 2015 14:20:00",
        "venue": "Strathfieldsaye",
        "homeTeam":
                {
                    "name": "Strathfieldsaye",
                    "goals": 10,
                    "points": 15
                },
        "awayTeam":
                {
                    "name": "Castlemaine",
                    "goals": 3,
                    "points": 6
                }
    },
    {
        "round": 4,
        "date": "Sat, 25 Apr 2015 14:20:00",
        "venue": "Gisborne",
        "homeTeam":
                {
                    "name": "Gisborne",
                    "goals": 9,
                    "points": 13
                },
        "awayTeam":
                {
                    "name": "Golden Square",
                    "goals": 11,
                    "points": 10
                }
    },
    {
        "round": 4,
        "date": "Sat, 25 Apr 2015 17:30:00",
        "venue": "Queen Elizabeth Oval",
        "homeTeam":
                {
                    "name": "South Bendigo",
                    "goals": 6,
                    "points": 11
                },
        "awayTeam":
                {
                    "name": "Kyneton",
                    "goals": 10,
                    "points": 11
                }
    },
    {
        "round": 5,
        "date": "Sat, 2 May 2015 14:20:00",
        "venue": "Castlemaine",
        "homeTeam":
                {
                    "name": "Castlemaine",
                    "goals": 10,
                    "points": 8
                },
        "awayTeam":
                {
                    "name": "Eaglehawk",
                    "goals": 9,
                    "points": 15
                }
    },
    {
        "round": 5,
        "date": "Sat, 2 May 2015 14:20:00",
        "venue": "Strathfieldsaye",
        "homeTeam":
                {
                    "name": "Strathfieldsaye",
                    "goals": 24,
                    "points": 24
                },
        "awayTeam":
                {
                    "name": "Maryborough",
                    "goals": 5,
                    "points": 2
                }
    },
    {
        "round": 5,
        "date": "Sat, 2 May 2015 14:20:00",
        "venue": "Golden Square",
        "homeTeam":
                {
                    "name": "Golden Square",
                    "goals": 17,
                    "points": 10
                },
        "awayTeam":
                {
                    "name": "Kyneton",
                    "goals": 7,
                    "points": 6
                }
    },
    {
        "round": 5,
        "date": "Sat, 2 May 2015 14:20:00",
        "venue": "Queen Elizabeth Oval",
        "homeTeam":
                {
                    "name": "South Bendigo",
                    "goals": 12,
                    "points": 19
                },
        "awayTeam":
                {
                    "name": "Gisborne",
                    "goals": 4,
                    "points": 6
                }
    },
    {
        "round": 5,
        "date": "Sat, 2 May 2015 17:30:00",
        "venue": "Kangaroo Flat",
        "homeTeam":
                {
                    "name": "Kangaroo Flat",
                    "goals": 7,
                    "points": 8
                },
        "awayTeam":
                {
                    "name": "Sandhurst",
                    "goals": 17,
                    "points": 24
                }
    },
    {
        "round": 6,
        "date": "Sat, 9 May 2015 14:20:00",
        "venue": "Maryborough",
        "homeTeam":
                {
                    "name": "Maryborough",
                    "goals": 6,
                    "points": 6
                },
        "awayTeam":
                {
                    "name": "Golden Square",
                    "goals": 27,
                    "points": 26
                }
    },
    {
        "round": 6,
        "date": "Sat, 9 May 2015 14:20:00",
        "venue": "Kangaroo Flat",
        "homeTeam":
                {
                    "name": "Kangaroo Flat",
                    "goals": 11,
                    "points": 9
                },
        "awayTeam":
                {
                    "name": "South Bendigo",
                    "goals": 8,
                    "points": 11
                }
    },
    {
        "round": 6,
        "date": "Sat, 9 May 2015 14:20:00",
        "venue": "Gisborne",
        "homeTeam":
                {
                    "name": "Gisborne",
                    "goals": 9,
                    "points": 4
                },
        "awayTeam":
                {
                    "name": "Eaglehawk",
                    "goals": 19,
                    "points": 17
                }
    },
    {
        "round": 6,
        "date": "Sat, 9 May 2015 14:20:00",
        "venue": "Kyneton",
        "homeTeam":
                {
                    "name": "Kyneton",
                    "goals": 5,
                    "points": 8
                },
        "awayTeam":
                {
                    "name": "Strathfieldsaye",
                    "goals": 20,
                    "points": 18
                }
    },
    {
        "round": 6,
        "date": "Sun, 10 May 2015 14:20:00",
        "venue": "Queen Elizabeth Oval",
        "homeTeam":
                {
                    "name": "Sandhurst",
                    "goals": 16,
                    "points": 17
                },
        "awayTeam":
                {
                    "name": "Castlemaine",
                    "goals": 3,
                    "points": 7
                }
    },
    {
        "round": 7,
        "date": "Sat, 16 May 2015 14:20:00",
        "venue": "Kangaroo Flat",
        "homeTeam":
                {
                    "name": "Kangaroo Flat",
                    "goals": 9,
                    "points": 12
                },
        "awayTeam":
                {
                    "name": "Strathfieldsaye",
                    "goals": 13,
                    "points": 11
                }
    },
    {
        "round": 7,
        "date": "Sat, 16 May 2015 14:20:00",
        "venue": "Castlemaine",
        "homeTeam":
                {
                    "name": "Castlemaine",
                    "goals": 18,
                    "points": 9
                },
        "awayTeam":
                {
                    "name": "Kyneton",
                    "goals": 21,
                    "points": 13
                }
    },
    {
        "round": 7,
        "date": "Sat, 16 May 2015 14:20:00",
        "venue": "Eaglehawk",
        "homeTeam":
                {
                    "name": "Eaglehawk",
                    "goals": 8,
                    "points": 11
                },
        "awayTeam":
                {
                    "name": "Golden Square",
                    "goals": 10,
                    "points": 14
                }
    },
    {
        "round": 7,
        "date": "Sat, 16 May 2015 14:20:00",
        "venue": "Gisborne",
        "homeTeam":
                {
                    "name": "Gisborne",
                    "goals": 1,
                    "points": 4
                },
        "awayTeam":
                {
                    "name": "Sandhurst",
                    "goals": 17,
                    "points": 25
                }
    },
    {
        "round": 7,
        "date": "Sat, 16 May 2015 14:20:00",
        "venue": "Queen Elizabeth Oval",
        "homeTeam":
                {
                    "name": "South Bendigo",
                    "goals": 25,
                    "points": 18
                },
        "awayTeam":
                {
                    "name": "Maryborough",
                    "goals": 5,
                    "points": 6
                }
    }

}

1 个答案:

答案 0 :(得分:0)

根据我的理解,如果结果不是对象,则应使用JSON.parse,而使用包含有效JSON的字符串变量。这样你就可以使用Javacript对象并做任何你喜欢的事情。