Javascript 如何避免在使用Java脚本在JSON中添加新项时添加一些字符

Javascript 如何避免在使用Java脚本在JSON中添加新项时添加一些字符,javascript,json,Javascript,Json,我试图用以下代码向JSON添加一个新项: var obj = JSON.parse(jsoncontent); obj["subnets"]["values"].push('{"gateway": "2.2.2.2","prefixLength": 24,"dnsSuffix": null,"dnsServer1": ""

我试图用以下代码向JSON添加一个新项:

var obj = JSON.parse(jsoncontent);
        
obj["subnets"]["values"].push('{"gateway": "2.2.2.2","prefixLength": 24,"dnsSuffix": null,"dnsServer1": "","dnsServer2": "","ipRanges":.... .bla bla bla');
    
Alert(JSON.stringify(obj,null,'\t'));
但是,正如您在下面看到的,新项目添加了“”和“\”字符。我怎样才能避免这种情况

{
    "gateway": "1.1.1.1",
    "prefixLength": 24,
    "dnsSuffix": null,
    "dnsServer1": "",
    "dnsServer2": "",
    "ipRanges": {
        "values": []
    },
    "enabled": false,
    "totalIpCount": 0,
    "usedIpCount": null,
    "primaryIp": null,
    "autoAllocateIpRanges": false
},
"{  \"gateway\": \"2.2.2.2\",  \"prefixLength\": 24,  \"dnsSuffix\": null,  \"dnsServer1\": \"\",  \"dnsServer2\": \"\",  \"ipRanges\": {    \"values\": []  },  \"enabled\": false,  \"totalIpCount\": 0,  \"usedIpCount\": null,  \"primaryIp\": null,  \"autoAllocateIpRanges\": false}"

在push函数中,附加字符串而不是对象。尝试删除此对象周围的引号

obj["subnets"]["values"].push({"gateway": "2.2.2.2","prefixLength": 24,"dnsSuffix": null,"dnsServer1": "","dnsServer2": ""});

好吧,你在追加一个字符串,所以所有的双引号都是给你的。只需删除第2行中的单引号。