大家好我想从JSON字符串的第一个和最后一个位置删除双引号,我需要将一个JSON对象放在另一个JSON对象中,我将显示我的代码和我的要求。
var sysMsg = new Object();
sysMsg.timestamp = "100";
sysMsg.seq = "5";
sysMsg.serviceid = "21";
sysMsg.srcip = "192.168.0.1";
sysMsg.ver = "1.0";
var sysString= JSON.stringify(sysMsg);
window.alert(sysString);
var systmMsg = new Object();
systmMsg.sys = sysString;
// JSON.stringify(hdrString);
var sysFinal= JSON.parse(systmMsg);
alert(sysFinal);
sysFinal = sysFinal.replace(/\\/g, "");
window.alert(sysFinal);
var hdrMsg = new Object();
hdrMsg.appcontext = "context";
hdrMsg.appname = "appName";
hdrMsg.appid = "identifier";
hdrMsg.cmd = "command";
hdrMsg.timeout = "500";
hdrMsg.option = "optionCommand";
var hdrString= JSON.stringify(hdrMsg);
//window.alert(hdrString);
var hdrFinal = new Object();
hdrFinal.hdr = hdrString;
var hdrFinalMsg= JSON.stringify(hdrFinal);
hdrFinalMsg = hdrFinalMsg.replace(/\\/g, "");
window.alert(hdrFinalMsg);
var dataMsg = new Object();
dataMsg.CntrlID = "6";
dataMsg.Devinst = "1";
dataMsg.btnstatus = "btnStatus";
dataMsg.DevID = "33";
dataMsg.value = "true";
dataMsg.status = "errorStatus";
var dataString = JSON.stringify(dataMsg);
//window.alert(dataString);
var dataFinal = new Object();
dataFinal.data = dataString;
var dataFinalMsg= JSON.stringify(dataFinal);
dataFinalMsg = dataFinalMsg.replace(/\\/g, "");
window.alert(dataFinalMsg);
var appData = hdrFinalMsg.concat(dataFinalMsg);
var appFinal = new Object();
appFinal.app = JSON.stringify(appData);
var appFinalMsg= JSON.stringify(appFinal);
appFinalMsg = appFinalMsg.replace(/\\/g, "");
window.alert(appFinalMsg);
var finalData = appFinalMsg.concat(sysFinal);
var finalMsg= JSON.stringify(finalData);
finalMsg = finalMsg.replace(/\\/g, "");
window.alert(finalMsg);
这里我得到的字符串如下
"{"app":""{"hdr":"{"appcontext":"context","appname":"appName","appid":"identifier","cmd":"command","timeout":"500","option":"optionCommand"}"}{"data":"{"CntrlID":"6","Devinst":"1","btnstatus":"btnStatus","DevID":"33","value":"true","status":"errorStatus"}"}""}{"sys":"{"timestamp":"100","seq":"5","serviceid":"21","srcip":"192.168.0.1","ver":"1.0"}"}"
但我希望输出如下
{"app":{"data":{"Cntrl.ID":"6","Dev.inst":"1","btn.status":"btnStatus","Dev.ID":"33","value":"true","status":"errorStatus"},"hdr":{"appcontext":"context","appname":"appName","appid":"identifier","cmd":"set","timeout":"500","option":"optionCommand"}},"sys":{"srcip":"192.168.0.1","ver":"1.0","serviceid":"21","seq":"5","timestamp":"100"}}
答案 0 :(得分:2)
我重构了你的代码,这样你就可以看看如何去除所有这些字符串操作:
如果您使用对象 - 只需使用它们并在您获得所需内容时序列化为JSON:
小提琴:https://jsfiddle.net/myppwgmb/
以下是我将如何做到这一点:
var sys = {
timestamp: "100",
seq: "5",
serviceid: "21",
srcip: "192.168.0.1",
ver: "1.0",
};
var hdr = {
appcontext: "context",
appname: "appName",
appid: "identifier",
cmd: "command",
timeout: "500",
option: "optionCommand",
};
var data = {
CntrlID: "6",
Devinst: "1",
btnstatus: "btnStatus",
DevID: "33",
value: "true",
status: "errorStatus",
}
var dataFinal = {
app: {
data: data,
hdr: hdr,
},
sys: sys
}
alert(JSON.stringify(dataFinal));
答案 1 :(得分:0)
你去吧
var yourString = "{JsonString}";
var result = yourString.substring(1, yourString.length-1);
或者您可以使用.slice
var result = yourString.slice(1, -1);