【问题标题】:Reformatting JSON by Placing Object into Array通过将对象放入数组来重新格式化 JSON
【发布时间】:2019-03-20 16:03:46
【问题描述】:

我正在尝试重新格式化我的 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 是在一个对象中,但我想把它放到一个数组中

我目前的想法是使用推送命令,但我不确定如何访问对象(循环?)。

【问题讨论】:

  • data.serviceConfigs = [data.serviceConfigs]?

标签: javascript json reformatting


【解决方案1】:

您可以访问密钥的值并将其以所需格式放回相同的密钥。

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    相关资源
    最近更新 更多