Я просмотрел несколько сообщений по этому вопросу, но не могу решить эту проблему. У меня был метод getData()
, который возвращал объект со свойством под названием results
. Это свойство содержало массив объектов следующим образом.
function getData() {
return {
"results": [{
"title": "Terminator: Dark Fate",
"poster_path": "/vqzNJRH4YyquRiWxCCOH0aXggHI.jpg",
"genre_ids": [28,878],
"shows": [0,1,2,3,4,5,6],
"backdrop_path": "/a6cDxdwaQIFjSkXf7uskg78ZyTq.jpg",
"overview": "More than two decades have passed since Sarah Connor prevented Judgment Day, changed the future, and re-wrote the fate of the human race. Dani Ramos is living a simple life in Mexico City with her brother and father when a highly advanced and deadly new Terminator – a Rev-9 – travels back through time to hunt and kill her. Dani's survival depends on her joining forces with two warriors: Grace, an enhanced super-soldier from the future, and a battle-hardened Sarah Connor. As the Rev-9 ruthlessly destroys everything and everyone in its path on the hunt for Dani, the three are led to a T-800 from Sarah’s past that may be their last best hope.",
"runtime": "2h14m",
"release_date": "2019-10-23"
},{
"title": "Fast & Furious Presents: Hobbs & Shaw",
"poster_path": "/kvpNZAQow5es1tSY6XW2jAZuPPG.jpg",
"genre_ids": [28,12,35],
"shows": [0,3,4],
"backdrop_path": "/qAhedRxRYWZAgZ8O8pHIl6QHdD7.jpg",
"overview": "Ever since US Diplomatic Security Service Agent Hobbs and lawless outcast Shaw first faced off, they just have swapped smacks and bad words. But when cyber-genetically enhanced anarchist Brixton's ruthless actions threaten the future of humanity, both join forces to defeat him.",
"runtime": "2h16m",
"release_date": "2019-08-01"
}]
};
}
Я хочу перенести те же данные в файл .json
и получить его через свойство results
и сам этот метод. Файл с именем data.json
, который я создал, является
[{
"title": "Terminator: ",
"poster_path": "/vqzNJRH4YyquRiWxCCOH0aXggHI.jpg",
"genre_ids": [28,878],
"shows": [0,1,2,3,4,5,6],
"backdrop_path": "/a6cDxdwaQIFjSkXf7uskg78ZyTq.jpg",
"overview": "More than two decades have passed since Sarah Connor prevented Judgment Day, changed the future, and re-wrote the fate of the human race. Dani Ramos is living a simple life in Mexico City with her brother and father when a highly advanced and deadly new Terminator – a Rev-9 – travels back through time to hunt and kill her. Dani's survival depends on her joining forces with two warriors: Grace, an enhanced super-soldier from the future, and a battle-hardened Sarah Connor. As the Rev-9 ruthlessly destroys everything and everyone in its path on the hunt for Dani, the three are led to a T-800 from Sarah’s past that may be their last best hope.",
"runtime": "2h14m",
"release_date": "2019-10-23"
},{
"title": "Fast & Furious Presents: Hobbs & Shaw",
"poster_path": "/kvpNZAQow5es1tSY6XW2jAZuPPG.jpg",
"genre_ids": [28,12,35],
"shows": [0,3,4],
"backdrop_path": "/qAhedRxRYWZAgZ8O8pHIl6QHdD7.jpg",
"overview": "Ever since US Diplomatic Security Service Agent Hobbs and lawless outcast Shaw first faced off, they just have swapped smacks and bad words. But when cyber-genetically enhanced anarchist Brixton's ruthless actions threaten the future of humanity, both join forces to defeat him.",
"runtime": "2h16m",
"release_date": "2019-08-01"
}]
Я также включил свойство results
в файл .json
, но он выдал Uncaught SyntaxError: Unexpected token ':'
Как получить содержимое файла .json
в Javascript? Я пытался использовать XMLHttpRequest
также, но не повезло! Спасибо.
РЕДАКТИРОВАТЬ После использования XMLHttpRequest:
function fetchJSONFile(path, callback) {
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
var data = JSON.parse(httpRequest.responseText);
if (callback) callback(data);
}
}
};
httpRequest.open('GET', path);
httpRequest.send();
}
fetchJSONFile('data.json', function(data){
/*
Outputs error on console:
VM540:43 Uncaught SyntaxError: Unexpected token / in JSON at position 2617
at JSON.parse (<anonymous>)
at XMLHttpRequest.httpRequest.onreadystatechange (app.js:253)
*/
console.log(data);
});