Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 需要格式化JSON输出以便与我的后端服务兼容_Javascript_Json - Fatal编程技术网

Javascript 需要格式化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" }

我有这样一个表单的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"
    }
  ]
}
与后端服务兼容

如何添加数组符号

您可以使用并检查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处传递字符串,那么它们也会作为数组添加。我已经根据这个问题编辑了我的问题