簡體   English   中英

通過將對象放入數組來重新格式化JSON

[英]Reformatting JSON by Placing Object into Array

我正在嘗試重新格式化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在對象中,但我希望將其放入數組中

我目前的想法是使用push命令,但是我不確定如何訪問該對象(循環?)。

您可以訪問鍵的值,並將其以所需的格式放回相同的鍵。

 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) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM