javascript - 通过将对象放入数组来重新格式化 JSON
问题描述
我正在尝试重新格式化我的 JSON 以使用外部 API
我当前的 JSON 结构:
{
"serviceConfigs": {
"servicecode": "SC1",
"specifiers": {
"Brand ID": {
"text": {
"value": "test",
"type": "text"
}
},
"Program ID": {
"text": {
"value": "test",
"type": "text"
}
}
}
}
}
期望的输出:
{
"serviceConfigs": [{
"servicecode": "SC1",
"specifiers": {
"Brand ID": {
"text": {
"value": "test",
"type": "text"
}
},
"Program ID": {
"text": {
"value": "test",
"type": "text"
}
}
}
}]
}
所以目前 serviceConfigs 在一个对象中,但我希望它进入一个数组
我目前的思考过程是使用推送命令,但我不确定如何访问对象(循环?)。
解决方案
您可以访问密钥的值并将其以所需格式放回相同的密钥。
let obj = {"serviceConfigs": {"servicecode": "SC1","specifiers": {"Brand ID": {"text": {"value": "test","type": "text"}},"Program ID": {"text": {"value": "test","type": "text"}}}}}
obj.serviceConfigs = [obj.serviceConfigs]
console.log(obj)
推荐阅读
- audio - ffmpeg 可以使用 pts_time 合并单独的 .webm 音频和视频文件吗?
- python - 更改堆积图上的标签
- android-studio - 如何修复 Android Studio 中灰显的导入模块?
- python - Discord.py — discord.ext.commands.errors.MemberNotFound 如果我尝试禁止不在服务器上的用户
- android-recyclerview - 尝试单击 recyclerview 中的按钮时,我的 recyclerview 出现问题
- graphql - 速度模板语言中的嵌套 foreach
- shopify - 如何将forlop中的标签附加到shopify液体中的数组
- javascript - 字符串中最后一个大写字母的正则表达式
- acumatica - 如何禁用库存 ID 字段的自动建议
- r - 如何根据R中另一列的字符串的一部分创建新的列值