如何使用AJAX方法将GithubGist中的JSON数据存储到数组中



我试图将JSON数据存储在一个变量中,稍后随机访问它。(随机超级英雄名称(

JSON数据是下面的GitHub要点

https://gist.githubusercontent.com/abroroo/004fa0a28b94bc7100b4f1bf53acb69d/raw/09fd4ca4ed1ddb21010a48502cf0846a844b658c/authors.json

我(第一次(尝试使用ajax方法,但我认为它不起作用。我在控制台中没有定义。

这是我的代码:

var namesData;
function getNames() {
return $.ajax({
headers: {
Accept: 'application/json'
},
url: 'https://gist.githubusercontent.com/abroroo/004fa0a28b94bc7100b4f1bf53acb69d/raw/09fd4ca4ed1ddb21010a48502cf0846a844b658c/authors.json',
success: function (jsonNames) {
namesData = JSON.parse(jsonNames);
console.log('namesData');
console.log(namesData);
}
});
}

以下是获取随机名称的功能


function getRandomName() {
return namesData.names[
Math.floor(Math.random() * namesData.names.length)
];
}
console.log(getRandomName());

感谢您的帮助!

我检查了你的gist-json文件,它有语法错误。源json是

[
"names": [
{
"superhero":"Batman", 
"publisher":"DC Comics", 
"alter_ego":"Bruce Wayne",
"first_appearance":"Detective Comics #27",
"characters":"Bruce Wayne"
},

array下不应该有密钥。所以把根数组改成像这个一样的dict

{
"names": [
{
"superhero":"Batman", 
"publisher":"DC Comics", 
"alter_ego":"Bruce Wayne",
"first_appearance":"Detective Comics #27",
"characters":"Bruce Wayne"
},

不能将Object放在Array主体中。您可以这样修改json。

{ 
"names":[
{
"superhero":"Batman", 
"publisher":"DC Comics", 
"alter_ego":"Bruce Wayne",
"first_appearance":"Detective Comics #27",
"characters":"Bruce Wayne"
},
{
"superhero":"Superman", 
"publisher":"DC Comics", 
"alter_ego":"Kal-El",
"first_appearance":"Action Comics #1",
"characters":"Kal-El"
},
{
"superhero":"Flash", 
"publisher":"DC Comics", 
"alter_ego":"Jay Garrick",
"first_appearance":"Flash Comics #1",
"characters":"Jay Garrick, Barry Allen, Wally West, Bart Allen"
},
{
"superhero":"Green Lantern", 
"publisher":"DC Comics", 
"alter_ego":"Alan Scott",
"first_appearance":"All-American Comics #16",
"characters":"Alan Scott, Hal Jordan, Guy Gardner, John Stewart, Kyle Raynor, Jade, Sinestro, Simon Baz"
},
{
"superhero":"Green Arrow", 
"publisher":"DC Comics", 
"alter_ego":"Oliver Queen",
"first_appearance":"More Fun Comics #73",
"characters":"Oliver Queen"
},
{
"superhero":"Wonder Woman", 
"publisher":"DC Comics", 
"alter_ego":"Princess Diana",
"first_appearance":"All Star Comics #8",
"characters":"Princess Diana"
},
{
"superhero":"Martian Manhunter", 
"publisher":"DC Comics", 
"alter_ego":"J'onn J'onzz",
"first_appearance":"Detective Comics #225",
"characters":"Martian Manhunter"
},
{
"superhero":"Robin/Nightwing", 
"publisher":"DC Comics", 
"alter_ego":"Dick Grayson",
"first_appearance":"Detective Comics #38",
"characters":"Dick Grayson"
},
{
"superhero":"Blue Beetle", 
"publisher":"DC Comics", 
"alter_ego":"Dan Garret",
"first_appearance":"Mystery Men Comics #1",
"characters":"Dan Garret, Ted Kord, Jaime Reyes"
},
{
"superhero":"Black Canary", 
"publisher":"DC Comics", 
"alter_ego":"Dinah Drake",
"first_appearance":"Flash Comics #86",
"characters":"Dinah Drake, Dinah Lance"
},
{
"superhero":"Spider Man", 
"publisher":"Marvel Comics", 
"alter_ego":"Peter Parker",
"first_appearance":"Amazing Fantasy #15",
"characters":"Peter Parker"
},
{
"superhero":"Captain America", 
"publisher":"Marvel Comics", 
"alter_ego":"Steve Rogers",
"first_appearance":"Captain America Comics #1",
"characters":"Steve Rogers"
},
{
"superhero":"Iron Man", 
"publisher":"Marvel Comics", 
"alter_ego":"Tony Stark",
"first_appearance":"Tales of Suspense #39",
"characters":"Tony Stark"
},
{
"superhero":"Thor", 
"publisher":"Marvel Comics", 
"alter_ego":"Thor Odinson",
"first_appearance":"Journey into Myster #83",
"characters":"Thor Odinson"
},
{
"superhero":"Hulk", 
"publisher":"Marvel Comics", 
"alter_ego":"Bruce Banner",
"first_appearance":"The Incredible Hulk #1",
"characters":"Bruce Banner"
},
{
"superhero":"Wolverine", 
"publisher":"Marvel Comics", 
"alter_ego":"James Howlett",
"first_appearance":"The Incredible Hulk #180",
"characters":"James Howlett"
},
{
"superhero":"Daredevil", 
"publisher":"Marvel Comics", 
"alter_ego":"Matthew Michael Murdock",
"first_appearance":"Daredevil #1",
"characters":"Matthew Michael Murdock"
},
{
"superhero":"Hawkeye", 
"publisher":"Marvel Comics", 
"alter_ego":"Clinton Francis Barton",
"first_appearance":"Tales of Suspense #57",
"characters":"Clinton Francis Barton"
},
{
"superhero":"Cyclops", 
"publisher":"Marvel Comics", 
"alter_ego":"Scott Summers",
"first_appearance":"X-Men #1",
"characters":"Scott Summers"
},
{
"superhero":"Silver Surfer", 
"publisher":"Marvel Comics", 
"alter_ego":"Norrin Radd",
"first_appearance":"The Fantastic Four #48",
"characters":"Norrin Radd"
} 
]
}

将括号更改为大括号

相关内容

  • 没有找到相关文章

最新更新