嵌套 JSON - 使用 React 检索参数

Nested JSON - Retrieve a paramater using React

提问人:Osman Remzi 提问时间:11/15/2023 更新时间:11/15/2023 访问量:37

问:

尝试检索 descriptionbannerUrl 等参数,但无济于事。有什么建议吗?似乎什么都没有输出......

如何获取这些值?

我尝试了以下方法,但没有运气:

import games from "../JSON/US.en.json";
{games.map((value,key)=>
                       <div>{value.description}</div>
        )}
[
{
    "70010000000025": {
        "**bannerUrl**": "xxxx",
        "category": [
            "Adventure",
            "Action",
            "RPG",
            "Other"
        ],
        "**description**": "xxxx",
        "developer": null,
        "frontBoxArt": null,
        "iconUrl": "https://img-eshop.cdn.nintendo.net/i/d3c210e61e8487200fc4c344987243a60257838187a69a6a81c42d7447d5d192.jpg",
        "id": "01007EF00011E000",
    }
]

根据上面的描述

反应JS JSON

评论


答:

0赞 maaajo 11/15/2023 #1

尝试:

{value["**bannerUrl**"]}

另请注意,您的数据嵌套在另一个对象中,70010000000025

    "70010000000025":
        "**bannerUrl**": "xxxx",
        "category": [
            "Adventure",
            "Action",
            "RPG",
            "Other"
        ],
        "**description**": "xxxx",
        "developer": null,
        "frontBoxArt": null,
        "iconUrl": "https://img-eshop.cdn.nintendo.net/i/d3c210e61e8487200fc4c344987243a60257838187a69a6a81c42d7447d5d192.jpg",
        "id": "01007EF00011E000",
    }
0赞 mandy8055 11/15/2023 #2

您的文件中的数据格式不正确。首先,您需要格式化它。然后,将 JSON 数据从以键作为属性的对象转换为对象数组。json..en.json

// en.json file content
{
  "70010000000025": {
    ...
  }
}

// App.jsx
const data = [games];

const gamesArray = Object.entries(data[0]).map(([key, value]) => ({
  key,
  ...value
}));

{gamesArray.map((game, index) => (
        <div key={index}>
          <div>Description: {game.description}</div>
          <div>Banner URL: {game.bannerUrl}</div>
        </div>
      ))}

代码演示