我是本机新手,我想创建一个Pokedex应用程序。
为了简单起见,我想在一个JSON文件中包含关于Pokemons的所有必要信息。
我使用while循环来调用REST API,该API包含了几乎所有我需要的信息。
目标是用API答案填充此JSON模板
{
"id_pokemon": "1",
"image": "./Images/pokemon/img/002.png",
"icon": "./Images/pokemon/ico/002.ico",
"name": "Slamèche",
"type": {
"0" :"feu" //=id_attack??
},
"stats":{
"pv":"39",
"att":"39",
"def": "39",
"satt": "39",
"sdef": "39",
"vit": "39"
},
"weakness": {
"":"",
"":"",
"":"",
"":"",
"":"",
"":"",
"":"",
"":"",
"":"",
"":"",
"":"",
"":"",
"":"",
"":"",
"":"",
"":"",
"":"",
"":""
},
"talent":{
"0": "id_talent", //Brasier
"1": "id_talent" //Force soleil - Talent caché
},
"caract": {
"masse": "10",
"capture_rate": "45",
"generation": "1",
"egg_group": {
"0": "id_egg_group", //Draconique
"1": "id_egg_group" //Monstrueuse
},
"mf_rate":"45", //45% de male donc (1-mf_rate)% de femelle
"eff_point": {
"stat": "att",
"value": "3"
},
"poke_familly": "id_familly",
"xp_10": "1000000"
},
"evolution":{
"howto": "Manière d'évoluer",
"to":{
"0": "id_pokemon",
"1": "id_pokemon"
}
},
"lieu":{
"0": {
"name": "name",
"precision": "Dans l'eau avec surf",
"rate": "rare"
},
"1": {
"name": "name",
"precision": "Dans l'eau avec surf",
"rate": "rare"
}
},
"attaque":{
"0": "id_att",
"1": "id_att",
"2": "id_att"
},
"qr_code": "./Images/pokemon/qr_code/002.png"
}
为完成此操作,我尝试在Javascipt中构建一个脚本,并将其放在此处。
function getPokemon() {
let i = 1
while (i < 5){
let url = "https://pokeapi.co/api/v2/pokemon/" + i
$.get (url, callBackSucces).done(function() {
})
.fail(function() {
alert("error");
})
.always(function() {
})
i++
};
console.log(i)
}
var callBackSucces = function(data) {
console.log(data);
var pokemon =
{
"id_pokemon": data.id,
"image": "./Images/pokemon/img/002.png",
"icon": "./Images/pokemon/ico/002.ico",
"name": data.name,
"type": {
"type1": data.types[0].type.name,
"type2": data.types[1].type.name
},
"stats": {
"pv":data.stats[5].base_stat,
"att": data.stats[4].base_stat,
"def": data.stats[3].base_stat,
"satt": data.stats[2].base_stat,
"sdef": data.stats[1].base_stat,
"vit": data.stats[0].base_stat
},
"weakness": {
"":"",
"":"",
"":"",
"":"",
"":"",
"":"",
"":"",
"":"",
"":"",
"":"",
"":"",
"":"",
"":"",
"":"",
"":"",
"":"",
"":"",
"":""
},
"talent":data.abilities.map(x =>
{return({
"name" : x.ability.name,
"hidden": x.is_hidden
})}),
"caract": {
"masse": "10",
"capture_rate": "45",
"generation": "1",
"egg_group": {
"0": "id_egg_group", //Draconique
"1": "id_egg_group" //Monstrueuse
},
"mf_rate":"45", //45% de male donc (1-mf_rate)% de femelle
"eff_point": {
"stat": "att",
"value": "3"
},
"poke_familly": "id_familly",
"xp_10": "1000000"
},
"evolution":{
"howto": "Manière d'évoluer",
"to":{
"0": "id_pokemon",
"1": "id_pokemon"
}
},
"lieu":{
"0": {
"name": "name",
"precision": "Dans l'eau avec surf",
"rate": "rare"
},
"1": {
"name": "name",
"precision": "Dans l'eau avec surf",
"rate": "rare"
}
},
"attaque":
data.moves.map(x => {
return({
"name": x.move.name,
"level": x.version_group_details[0].level_learned_at
})
}),
"qr_code": "./Images/pokemon/qr_code/002.png"
}
var myJson = JSON.stringify(pokemon)
console.log(myJson)
document.getElementById("demo").innerHTML = myJson;
}
我没有问题可以捕获第一级数据,但是一些答案包含了这一点
'''location_area_encounters:“ https://pokeapi.co/api/v2/pokemon/4/encounters”'''
这对我来说开始变得很困难,我该如何通过一次性功能获得这些信息?
为了让您理解,我将API的答案用于了神奇宝贝的呼叫。
abilities: Array(2)
0: {ability: {…}, is_hidden: true, slot: 3}
1: {ability: {…}, is_hidden: false, slot: 1}
length: 2
__proto__: Array(0)
base_experience: 62
forms: Array(1)
0:
name: "charmander"
url: "https://pokeapi.co/api/v2/pokemon-form/4/"
__proto__: Object
length: 1
__proto__: Array(0)
game_indices: Array(20)
0: {game_index: 4, version: {…}}
1: {game_index: 4, version: {…}}
2: {game_index: 4, version: {…}}
3: {game_index: 4, version: {…}}
4: {game_index: 4, version: {…}}
5: {game_index: 4, version: {…}}
6: {game_index: 4, version: {…}}
7: {game_index: 4, version: {…}}
8: {game_index: 4, version: {…}}
9: {game_index: 4, version: {…}}
10: {game_index: 4, version: {…}}
11: {game_index: 4, version: {…}}
12: {game_index: 4, version: {…}}
13: {game_index: 4, version: {…}}
14: {game_index: 4, version: {…}}
15: {game_index: 4, version: {…}}
16: {game_index: 4, version: {…}}
17: {game_index: 176, version: {…}}
18: {game_index: 176, version: {…}}
19: {game_index: 176, version: {…}}
length: 20
__proto__: Array(0)
height: 6
held_items: Array(0)
length: 0
__proto__: Array(0)
id: 4
is_default: true
location_area_encounters: "https://pokeapi.co/api/v2/pokemon/4/encounters"
moves: Array(93)
0: {move: {…}, version_group_details: Array(4)}
1: {move: {…}, version_group_details: Array(7)}
2: {move: {…}, version_group_details: Array(4)}
3: {move: {…}, version_group_details: Array(18)}
4: {move: {…}, version_group_details: Array(19)}
5: {move: {…}, version_group_details: Array(16)}
6: {move: {…}, version_group_details: Array(4)}
7: {move: {…}, version_group_details: Array(3)}
8: {move: {…}, version_group_details: Array(5)}
9: {move: {…}, version_group_details: Array(2)}
...
length: 93
__proto__: Array(0)
name: "charmander"
order: 5
species:
name: "charmander"
url: "ADRESS"
__proto__: Object
sprites:
back_default: "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/back/4.png"
back_female: null
back_shiny: "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/back/shiny/4.png"
back_shiny_female: null
front_default: "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/4.png"
front_female: null
front_shiny: "ADRESS"
front_shiny_female: null
__proto__: Object
stats: Array(6)
0: {base_stat: 65, effort: 1, stat: {…}}
1: {base_stat: 50, effort: 0, stat: {…}}
2: {base_stat: 60, effort: 0, stat: {…}}
3: {base_stat: 43, effort: 0, stat: {…}}
4: {base_stat: 52, effort: 0, stat: {…}}
5: {base_stat: 39, effort: 0, stat: {…}}
length: 6
__proto__: Array(0)
types: Array(1)
0: {slot: 1, type: {…}}
length: 1
__proto__: Array(0)
weight: 85
总结:
我想基于Pokemon API创建一个包含Pokemon的所有信息的JSON文件。
我可以捕获第一级数据,但是如果使用API地址,则无法达到第二级数据。
下一步,我想在我的App中创建一个函数,如果答案已更改,该函数将查看每次启动,如果是,则实现文档。
感谢您的帮助!