json 我如何获取、访问数据?

xqkwcwgp  于 2023-05-19  发布在  其他
关注(0)|答案(1)|浏览(219)

我有一个嵌套元素的JSON文件,我只是不知道如何显示文件中的数据。
我尝试使用Object.keys(),Object.values()和Object.entries(),但我得到的都是错误或什么都不显示。
我有以下JSON文件和React代码

{
  "data": {
    "finishedPercentage": 87.313,
    "inProgress": 234,
    "activeUsers": {
      "firstUser": [
        {
          "id": 6091438,
          "gpus": { "count": 2, "model": "NVIDIA GeForce RTX 3090" },
          "updatedAt": "2023-05-14T12:34:49.268Z",
          "mSpeed": 386
        },
        {
          "id": 6091453,
          "gpus": { "count": 4, "model": "NVIDIA GeForce RTX 3090" },
          "updatedAt": "2023-05-14T12:34:31.060Z",
          "mSpeed": 751
        }
      ],
      "secondUser": [
        {
          "id": 6208888,
          "gpus": { "count": 3, "model": "NVIDIA GeForce RTX 3080 Ti" },
          "updatedAt": "2023-05-14T12:34:36.482Z",
          "mSpeed": 542
        },
        {
          "id": 6158802,
          "gpus": { "count": 3, "model": "NVIDIA GeForce RTX 3090" },
          "updatedAt": "2023-05-14T12:35:34.211Z",
          "mSpeed": 558
        }
       ]
     }
  }
}
function App() {
  const [data, setData] = useState({});

  useEffect(() => {
    fetch("http://localhost:8000/data")
      .then((response) => response.json())
      .then((data) => {
        setData(data);
      });
  }, []);

  return (
    <div className="App">
      <Cards data={data} />
    </div>
  );
}
const Cards = ({ data }) => {
  let firstUser = Object.entries(data || data?.activeUsers?.firstUser);

  return (
    <div>
      {firstUser.map((el) => (
        <h1 key={el.id}>{el.updateAt}</h1>
      ))}
    </div>
  );
};

export default Cards;
6ss1mwsb

6ss1mwsb1#

我想你有点困惑的第一个data prop 。您提供的数据本身有一个键“data”,当您将此对象传递给Cardsdata prop并破坏prop时,您不会破坏对象中的data键。因此,您必须使用data.data访问它。您还拼错了“updateAt”,请注意我在下面的代码片段中将其更改为“updatedAt”。

const Cards = ({ data }) => {
  return (
    <div>
      {data.data && data.data.activeUsers.firstUser.map((el) => (
        <h1 key={el.id}>{el.updatedAt}</h1>
      ))}
    </div>
  );
};

ReactDOM.render(
    <Cards data={{
  "data": {
"finishedPercentage": 87.313,
"inProgress": 234,
"activeUsers": {
  "firstUser": [
    {
      "id": 6091438,
      "gpus": { "count": 2, "model": "NVIDIA GeForce RTX 3090" },
      "updatedAt": "2023-05-14T12:34:49.268Z",
      "mSpeed": 386
    },
    {
      "id": 6091453,
      "gpus": { "count": 4, "model": "NVIDIA GeForce RTX 3090" },
      "updatedAt": "2023-05-14T12:34:31.060Z",
      "mSpeed": 751
    }
  ],
  "secondUser": [
    {
      "id": 6208888,
      "gpus": { "count": 3, "model": "NVIDIA GeForce RTX 3080 Ti" },
      "updatedAt": "2023-05-14T12:34:36.482Z",
      "mSpeed": 542
    },
    {
      "id": 6158802,
      "gpus": { "count": 3, "model": "NVIDIA GeForce RTX 3090" },
      "updatedAt": "2023-05-14T12:35:34.211Z",
      "mSpeed": 558
    }
   ]
}
  }
}} />,
    document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script>

相关问题