我想改变API响应JSON格式



这是我从API得到的实际响应,我想转换以下JSON格式

来自:

"data": [
[
{
"isFile": "true",
"fileType": "image",
"filesize": 100793,
"filename": "attachment_0.png",
}
],
[
{
"isFile": "true",
"fileType": "image",
"filesize": 6343078,
"filename": "attachment_1.png"
}
]
]

:

"data": [
{
"isFile": "true",
"fileType": "image",
"filesize": 100793,
"filename": "attachment_0.png",
},
{
"isFile": "true",
"fileType": "image",
"filesize": 6343078,
"filename": "attachment_1.png"
}
]

如何删除对象之间的数组。

  1. 将JSON转换为JavaScript
  2. 平坦数组
  3. 将JavaScript转换为Json
const json = JSON.stringify(JSON.parse(data).flat())

I had to fix your JSON:

const data = `[
[
{
"isFile": "true",
"fileType": "image",
"filesize": 100793,
"filename": "attachment_0.png"
}
],
[
{
"isFile": "true",
"fileType": "image",
"filesize": 6343078,
"filename": "attachment_1.png"
}
]
]`

const json = JSON.stringify(JSON.parse(data).flat());
console.log(json);

您想要的所有内容都在data数组的第一个元素中,因此只需索引它。

obj = JSON.parse(response);
obj.data = obj.data[0];

如果响应不是很大,你可以做

let data = JSON.parse(response);
for (el in data) {
el = el[0]
}

否则,请看这里

最新更新