javascript - 获取 JSON API 中的所有数组
问题描述
我正在使用 Promise 样式从 API 获取 JSON 数组。但是,我想获取 JSON URL 中的所有数组。是否有使用 useEffects for React 的简单解决方案?此外,我是否可以在 useEffect 中动态搜索 JSON 数组,是否应该恢复使用 redux 并从用户查询中获取快照。
JSON 数组示例:
"shirt": [
{
"shirt": "red
"price": 3
}
{
"shirt": "red
"price": 3
}
]
"hat": [
{
"shirt": "red
"price": 4
}
] .....
我目前的解决方案:
const [data, setData] = useState([]);
useEffect(() => {
fetch(productURL)
.then(response => response.json())
// .then(data => data.json())
.then(json => setData(json.sneakers))
.catch(error => alert(error))
.finally(setLoading(false));
}, []);```
解决方案
推荐阅读
- .net - 当响应是原始字符串时,无法反序列化 Restsharp 响应
- php - PHP_SELF 和 DOCUMENT_ROOT 因浏览器而异
- python - 按创建或修改日期复制文件
- python - ValueError: int() 以 10 为底的无效文字:'"s21101180756377t}?S<78O#!R1GdO)te19936004699339'
- javascript - 如何从 fetch 和 Promises 中获取 HTTP 错误详细信息和文本?
- javascript - 嵌套私有路由的私有组件
- javascript - 如何修复由 react-redux 中的循环依赖引起的“不变违规”
- javascript - 如何在nodejs中发布请求后在html上显示响应消息
- javascript - 为什么 video.width 是 0 但实际上是 539?
- python - 很想在 Maya 中创建一个循环遍历场景中所有透视摄像机的 Python 脚本?