Javascript 需要格式化JSON输出以便与我的后端服务兼容
我有这样一个表单的JSON输出Javascript 需要格式化JSON输出以便与我的后端服务兼容,javascript,json,Javascript,Json,我有这样一个表单的JSON输出 { "testRefs": { "testCd": null, "testIndicator": null, "testInd": null, "testiolInd": null }, "testList": { "testname": null, "testcode": null }, "testCd": "someStrinf", "testNm": "someString" }
{
"testRefs": {
"testCd": null,
"testIndicator": null,
"testInd": null,
"testiolInd": null
},
"testList": {
"testname": null,
"testcode": null
},
"testCd": "someStrinf",
"testNm": "someString"
}
我需要将表单输出格式化为
{
"testRefs": [
{
"testCd": null,
"testIndicator": null,
"testInd": null,
"testiolInd": null
}],
{
"testList": [
{
"testname": null,
"testcode": null
}
],
"testCd": "someStrinf",
"testNm": "someString"
}
]
}
与后端服务兼容
如何添加数组符号您可以使用并检查obj[key]处的键长度,如果它大于1,只需将其添加到数组中,否则保持不变
设obj={testRefs:{testCd:null,testIndicator:null,testInd:null,testiold:null},testList:{testname:null,testcode:null},testCd:null,testNm:null}
obj中的forlet键{
ifObject.keysobj[key]| |[]长度>1{
obj[键]=[obj[键]]
}
}
console.logobj如果在testCd/testNm处传递字符串,那么它们也会作为数组添加。我已经根据这个问题编辑了我的问题