如何在javascript中显示Json对象

时间:2014-06-12 18:13:22

标签: javascript html

我想在javascript中显示json文件中的数据。我的Json文件如下:

{
    "structure": [
        {
            "country": "Ecuador",
            "sport": "soccer",
            "id": "545",
            "age": "23",
            "gender": "male"
        },
        {
            "country": "USA",
            "sport": "Golf",
            "age": "22",
            "id": "2",
            "gender": "female"
        },
        {
            "gender": "male",
            "id": "3",
            "sport": "Basketball",
            "age": "21",
            "country": "Argentina"
        },
        {
            "gender": "female",
            "sport": "Tennis",
            "id": "4",
            "age": "20",
            "country": "Colombia"
        },
        {
            "country": "Venezuela",
            "gender": "male",
            "age": "19",
            "sport": "Volleyball",
            "id": "5"
        }
    ]
}

我的javascript请求json文件如下:

var setup = function() {
    var xhr = new XMLHttpRequest();
   async = true;
    xhr.open("GET", "output.json", async);

    xhr.onload = function(){

        var finalResult = JSON.parse(this.responseText);
        console.log(finalResult['structure']);
    };

 /*kick off the request*/
    xhr.send();
};

有人可以帮我显示实际数据吗?

1 个答案:

答案 0 :(得分:0)

JSON已经是文字了。在解析之前记录它:console.log(this.responseText)