javascript - 解析 JSON 数组响应
问题描述
我从一个 API 收到的 JSON 响应返回的格式不正确,无法解析。
我试图在开始时添加缺少的密钥,但它不允许。
[
{
"deviceId": "9092eab10f4",
"name": "temperature",
"timestamp": "2017-06-13T13:19:59.673Z",
"value": 21.5
},
{
"deviceId": "9092eab10f4",
"name": "temperature",
"timestamp": "2017-06-13T13:19:59.673Z",
"value": 21.5
}
]
我希望它有丢失的键和额外的大括号,如下所示:
{
"data": [
{
"deviceId": "9092eab10f4",
"name": "temperature",
"timestamp": "2017-06-13T13:19:59.673Z",
"value": 21.5
},
{
"deviceId": "9092eab10f4",
"name": "temperature",
"timestamp": "2017-06-13T13:19:59.673Z",
"value": 21.5
}
]
}
解决方案
简单的对象分配?
const properResponse = Object.assign({}, {data: [response.json()]});
...假设响应是 fetch,或者与返回响应对象的 json 方法类似。
推荐阅读
- azure - 如何从 Blob 存储下载文件
- java - 拉出字符串拆分的多个部分
- tensorflow - 什么对卡片识别更有效?整张卡片的文字和小图像或图像?
- javascript - 如何避免在 javascript 单行“if”语句中使用重复的措辞
- python - 将对象转换为日期时间时出错
- vba - 使用 VBA 将图像插入 Word
- java - 捕获 Wildfly 关闭挂钩
- angular - 为什么 Angular 应用程序生产构建的本地服务器需要 /index.html 来加载应用程序
- autodesk-forge - BIM 360 检查权限
- r - 如何在ggplot中使直方图条轮廓透明?