简体   繁体   English

轻松添加多个级别的深度JSON

[英]Add multiple levels deep JSON easily

I'm creating a json to get interpreted for dialog creation. 我正在创建一个json,以便为对话框创建进行解释。

My original attempt is something like this: 我最初的尝试是这样的:

var dialog = {};
dialog.warning.nextEle["key"] = "value";

hoping that it would just generate the 'warning' and 'nextEle'. 希望它会生成“警告”和“ nextEle”。 I could do something like this: 我可以做这样的事情:

dialogMsg["warning"] = {"nextEle" : {"key" : "value"}};

but what I'm wanting is to be able to make it add to dialog.warning.nextEle if it already exists and add that depth if it doesn't already exist... ie: 但是我想要的是能够将它添加到dialog.warning.nextEle(如果已经存在),并添加该深度(如果还不存在...):

{} would become {} 会成为

{ "warning" : { "nextEle" : { "key" : "value"}}}

and using the same format, I could add to make it 并使用相同的格式,我可以添加使其

{ "warning" : { "nextEle" : { "key" : "value", "key2" : "value2"}}}

is it possible to do this without using conditionals? 是否可以在不使用条件的情况下做到这一点?

If you are using jQuery, make use of the extend function, it will add the necessary components automatically. 如果您使用的是jQuery,请使用extend功能,它将自动添加必要的组件。

$.extend(true, dialog,{ 
    "warning" : { 
        "nextEle" : { 
            "key" : "value"
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM