reactjs 嵌套JSON -使用React调用参数

mrphzbgm  于 2023-11-18  发布在  React
关注(0)|答案(2)|浏览(117)

尝试检索参数,如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",
    }
]

根据上述描述

7rtdyuoh

7rtdyuoh1#

您的..en.json文件中的json数据格式不正确。首先您需要格式化它。然后您将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>
      ))}

字符串

nkoocmlb

nkoocmlb2#

试试看:

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

字符串
另外请注意,您的数据嵌套在另一个对象中7001000000025

"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",
    }

相关问题