Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 需要帮助理解为什么我';使用JSON.parse()需要m_Javascript_Ajax_Json - Fatal编程技术网

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

Javascript 需要帮助理解为什么我';使用JSON.parse()需要m,javascript,ajax,json,Javascript,Ajax,Json,我目前的web开发课程有一个作业,我的问题是我不明白为什么我需要使用JSON.parse()(尽管这是一个要求)。我可以猜测它将在哪里被使用,但通过实验还没有得到任何结果 一般的想法是,我们在一个单独的文件(显然是在同一台服务器上)中从JSON数组中提取信息。这是通过讲师编写的ajax.js文件中包含的函数完成的,该文件相对简单,如下所示: function getJSON(url, callbackFunction) { //var request = new XMLHttpRequest(

我目前的web开发课程有一个作业,我的问题是我不明白为什么我需要使用
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);
}

}
var parsedData = JSON.parse(gamesArray);
alert(parsedData[1]);
JSON.parse()
添加代码时,即使在文档末尾添加额外的代码,也不会发生任何事情。守则如下:

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);
}

}
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
                }
    }

}

据我所知,如果结果不是对象,而是包含有效JSON的字符串变量,则应该使用JSON.parse。通过这种方式,您可以使用JavaScript对象并执行任何您喜欢的操作

请发布指定为作业要求的getJSON代码,如原始问题中所述。您可能需要
gameArray.data[counter]
-您可以向我们展示实际的JSON内容吗?您应该询问讲师为什么需要它,因为他提供的函数似乎已经在为您将响应从字符串转换为对象(
responseType='json'
)。不过,这里似乎没有技术原因,所以他是唯一能够实际回答的人。如果您得到的响应是一个普通字符串,则需要在回调开始时将
结果传递给它。。。但事实并非如此。