我想要一个 json 对象,但得到多个。
let example = {
test1: '',
test2: '',
oTest: {
oTest1: '',
oTest2: ''
},
ooTest: {
ooTest1: '',
ooTest2: null,
}
};
let result = JSON.stringify({
example,
'oTest': example.oTest,
'ooTest': example.ooTest
});
console.log(JSON.stringify(example));
我的结果是我得到 3 个独立的 Json 对象(例如,oTest,ooTest),但我的目标是拥有 1 个 Json 对象“示例”,包括 oTest 和 ooTest。另外“示例”不显示 oTest 和 ooTest。
目标:
{
"test1": "",
"test2": "",
"oTest": {
"oTest1": "",
"oTest2": ""
},
"ooTest": {
"ooTest1": "",
"ooTest2": ""
}
}
最佳答案
你所做的事情根本没有意义。只需将 example
传递给 JSON.stringify
JSON.stringify(example);
关于javascript - 用对象中的对象构建Json(JS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56126788/