访问 response.json 个数据数组

Accessing response.json data arrays

我找遍了所有地方,但找不到这个问题的答案。

RAWG 是一个游戏评论网站,现在有自己的 API(而不是使用 Rapid API),我想要“汤姆克兰西彩虹六号围攻”的数据。

我将请求中的数据 return 转换为 response.json(使用下面的代码),但我无法访问游戏“Tom Clancy Rainbow Six Siege”。

fetch("https://api.rawg.io/api/games?key=[KEY]&developers=ubisoft", {
    method: 'GET'
})

// Gets the response from the website
.then(response => response.json())
// Uses the data in the json to populate the website with information
.then(data => {
  console.log(data);
  for (const game in data.results) {
    if (game == 6) {
        console.log(game[0]['id']);
    }
  }
});

API return(来自console.log):

{count: 305, next: 'https://api.rawg.io/api/games?developers=ubisoft&key=[key]&page=2', previous: null, results: Array(20), user_platforms: false}
count: 305
next: "https://api.rawg.io/api/games?developers=ubisoft&key=[key]&page=2"
previous: null
results: Array(20)
0: {slug: 'far-cry-3', name: 'Far Cry 3', playtime: 16, platforms: Array(5), stores: Array(4), …}
1: {slug: 'for-honor', name: 'For Honor', playtime: 7, platforms: Array(3), stores: Array(4), …}
2: {slug: 'watch-dogs', name: 'Watch Dogs', playtime: 19, platforms: Array(6), stores: Array(6), …}
3: {slug: 'far-cry-5', name: 'Far Cry 5', playtime: 19, platforms: Array(3), stores: Array(3), …}
4: {slug: 'assassins-creed-odyssey', name: "Assassin's Creed Odyssey", playtime: 32, platforms: Array(4), stores: Array(5), …}
5: {slug: 'assassins-creed-origins', name: "Assassin's Creed Origins", playtime: 35, platforms: Array(3), stores: Array(3), …}
6: {slug: 'rayman-legends', name: 'Rayman Legends', playtime: 5, platforms: Array(8), stores: Array(6), …}
7:
added: 5061
added_by_status: {yet: 93, owned: 3811, beaten: 244, toplay: 56, dropped: 533, …}
background_image: "https://media.rawg.io/media/games/b34/b3419c2706f8f8dbe40d08e23642ad06.jpg"
clip: null
dominant_color: "0f0f0f"
esrb_rating: {id: 3, name: 'Teen', slug: 'teen', name_en: 'Teen', name_ru: 'С 13 лет'}
genres: (2) [{…}, {…}]
id: 8488
metacritic: 75
name: "Tom Clancy's Rainbow Six Siege"
parent_platforms: (3) [{…}, {…}, {…}]
platforms: (5) [{…}, {…}, {…}, {…}, {…}]
playtime: 29
rating: 3.99
rating_top: 4
ratings: (4) [{…}, {…}, {…}, {…}]
ratings_count: 1076
released: "2015-12-01"
reviews_count: 1085
reviews_text_count: 5
saturated_color: "0f0f0f"
score: null
short_screenshots: (7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
slug: "tom-clancys-rainbow-six-siege-2"
stores: (4) [{…}, {…}, {…}, {…}]
suggestions_count: 466
tags: (16) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
tba: false
updated: "2021-02-17T15:00:09"
user_game: null
[[Prototype]]: Object

我想访问#7(彩虹六号围攻)并获取 ID 和名称。但是对于我的一生,我无法弄清楚。 for 循环只是 return 数字 6,我无法访问其中的任何数据。

好的,经过一些挖掘,我想到了这个。

您的搜索参数应该是 search= 您要查找的标题,search_exact 应该是 true.

search=tom-clancys-rainbow-six-siege&search_exact=true

应该是return第1个数据,可以访问第一个results数组元素的object中的信息。

const url = 'https://api.rawg.io/api/games?developers=ubisoft&key={your key}&search=tom-clancys-rainbow-six-siege&search_exact=true';

async function getData() {
  const json = await fetch(url);
  const data = await json.json();
  const { id, name } = data.result[0];
  console.log(id, name);
}