【发布时间】:2025-12-13 04:30:01
【问题描述】:
我有 products.json,其中有数据。现在,我希望使用 Reactjs 渲染它。
products.json
[
{
"id": 1,
"productName": "Fjallraven - Foldsack No. 1 Backpack, Fits 15 Laptops",
"price": 109.95,
"description": "Your perfect pack for everyday use and walks in the forest. Stash your laptop (up to 15 inches) in the padded sleeve, your everyday",
"category": "men's clothing",
"image": "https://fakestoreapi.com/img/81fPKd-2AYL._AC_SL1500_.jpg",
"specification": {}
},
{
"id": 2,
"productName": "Mens Casual Premium Slim Fit T-Shirts ",
"price": 22.3,
"description": "Slim-fitting style, contrast raglan long sleeve, three-button henley placket, light weight & soft fabric for breathable and comfortable wearing. And Solid stitched shirts with round neck made for durability and a great fit for casual fashion wear and diehard baseball fans. The Henley style round neckline includes a three-button placket.",
"category": "men's clothing",
"image": "https://fakestoreapi.com/img/71-3HjGNDUL._AC_SY879._SX._UX._SY._UY_.jpg",
"specification": {}
}
]
app.js
function App(){
return(
)
}
我希望通过 app.js 呈现 json 数据。
我的看法:
我是 Reactjs 和 JSON 的新手,正在考虑使用 fetch、response,但我不确定我该怎么做。
有人可以帮忙吗?
【问题讨论】:
-
我认为@stekhn 评论应该有助于在反应中呈现这个,但你也提到了 fetch - 这可能有助于developer.mozilla.org/en-US/docs/Web/API/Fetch_API
-
fetch() 用于从服务器加载数据。使用本地 json 文件,您可以将其导入:codesandbox.io/s/upbeat-nightingale-098lx?file=/src/App.js
-
假设我在 json 中有类别。如何根据类别进行不同的渲染?
标签: javascript json reactjs