问题遇到的现象和发生背景
我循环对象里面的数据,对象里面有两行数据,我每次循环出来取的都是最后一行,而且push出来的值还多了。
问题相关代码,请勿粘贴截图
//读取选中行的数据
if(chk!=undefined && chk.checked){
//一个查询
var param = getSearchParams();
for (var i = 0; i <param.length; i++) {
//item
var ZYL02 = param[i].ZYL02;
var ZYL01 = param[i].ZYL01;
var ZID = param[i].ZID;
var DATUV = "20220419";
var WERKS = param[i].WERKS;
var AETXT = param[i].AETXT;
var AENNR = param[i].AENNR;
//items
var ZWYBS = param[i].ZWYBS;
var ZMESVIN = param[i].ZMESVIN;
var ZMESDATE = param[i].ZMESDATE;
var IDNRK = param[i].IDNRK;
var ZYL04 = param[i].ZYL04;
var SORTF = "X";
var ZYL06 = param[i].ZYL06;
var ZCZZT = param[i].ZCZZT;
var AUSCH = "";
var ZYL05 = param[i].ZYL05;
var ZYL03 = param[i].ZYL03;
var MATNR = param[i].MATNR;
// Items
item_list.ZYL02 = ZYL02;
item_list.ZYL01 = ZYL01;
item_list.ZID = ZID;
item_list.DATUV = DATUV;
item_list.WERKS = WERKS;
item_list.AETXT = AETXT;
item_list.AENNR = AENNR;
item_list.ZYL03 = ZYL03;
item_list.MATNR = MATNR;
//item_list.ITEM = items_list;
items_list.ZWYBS = ZWYBS;
items_list.ZMESVIN = ZMESVIN;
items_list.ZMESDATE = ZMESDATE;
items_list.IDNRK = IDNRK;
items_list.ZYL04 = ZYL04;
items_list.SORTF = SORTF;
items_list.ZYL06 = ZYL06;
items_list.ZCZZT = ZCZZT;
items_list.AUSCH = AUSCH;
items_list.ZYL05 = ZYL05;
//创建最外层响应头
req.REQUEST = {};
//赋值
data.HEAD = head;
data.DATA = REQUEST;
REQUEST.ITEM = item;
main.ITEMS = items;
req.REQUEST = data;
//追加
item_list.ITEMS = items;
item.push(item_list);
items.push(items_list);
}
var JsonData = JSON.stringify(req);
运行结果及报错内容
得到的json字符串数据
{
"REQUEST": {
"HEAD": {
"ACCOUNT": "ESB_TEST_MES",
"PASSWORD": "@esb",
"CONSUMER": "SAP",
"COUNT": "1",
"TRANSACTIONID": "",
"SRVLEVEL": "1",
"COMPANY": "BAW"
},
"DATA": {
"ITEM": [
{
"ZYL02": "",
"ZYL01": "",
"ZID": "LL0125001",
"DATUV": "20220419",
"WERKS": "1081",
"AETXT": "VIN",
"AENNR": "MCN00000041",
"ZYL03": "",
"MATNR": "LL0125",
"ITEMS": [
{
"ZWYBS": "0PtNBcLR5dHOAA",
"ZMESVIN": "SVU20220711LEDAAA",
"ZMESDATE": "2022-09-01",
"IDNRK": "000000010021000697",
"ZYL04": "",
"SORTF": "X",
"ZYL06": "",
"ZCZZT": "U",
"AUSCH": "",
"ZYL05": ""
},
{
"ZWYBS": "Fi08398e93ddc",
"ZMESVIN": "SVU20220711LED2222",
"ZMESDATE": "2022-09-01",
"IDNRK": "000000010021000697",
"ZYL04": "",
"SORTF": "X",
"ZYL06": "",
"ZCZZT": "U",
"AUSCH": "",
"ZYL05": ""
}
]
},
{
"ZYL02": "",
"ZYL01": "",
"ZID": "LL0125001",
"DATUV": "20220419",
"WERKS": "1081",
"AETXT": "VIN",
"AENNR": "MCN00000041",
"ZYL03": "",
"MATNR": "LL0125",
"ITEMS": [
{
"ZWYBS": "0PtNBcLR5dHOAA",
"ZMESVIN": "SVU20220711LEDAAA",
"ZMESDATE": "2022-09-01",
"IDNRK": "000000010021000697",
"ZYL04": "",
"SORTF": "X",
"ZYL06": "",
"ZCZZT": "U",
"AUSCH": "",
"ZYL05": ""
},
{
"ZWYBS": "0PtNBcLR5dHOAA",
"ZMESVIN": "SVU20220711LEDAAA",
"ZMESDATE": "2022-09-01",
"IDNRK": "000000010021000697",
"ZYL04": "",
"SORTF": "X",
"ZYL06": "",
"ZCZZT": "U",
"AUSCH": "",
"ZYL05": ""
}
]
}
]
}
}
}
实际上得到的正确json字符串如下:
{
"REQUEST": {
"HEAD": {
"ACCOUNT": "ESB_TEST_MES",
"PASSWORD": "@esb",
"CONSUMER": "SAP",
"COUNT": "1",
"TRANSACTIONID": "",
"SRVLEVEL": "1",
"COMPANY": "BAW"
},
"DATA": {
"ITEM": [
{
"ZYL02": "",
"ZYL01": "",
"ZID": "LL0125001",
"DATUV": "20220419",
"WERKS": "1081",
"AETXT": "VIN",
"AENNR": "MCN00000041",
"ZYL03": "",
"MATNR": "LL0125",
"ITEMS": [
{
"ZWYBS": "0PtNBcLR5dHOAA",
"ZMESVIN": "SVU20220711LEDAAA",
"ZMESDATE": "2022-09-01",
"IDNRK": "000000010021000697",
"ZYL04": "",
"SORTF": "X",
"ZYL06": "",
"ZCZZT": "U",
"AUSCH": "",
"ZYL05": ""
},
{
"ZWYBS": "Fi08398e93ddc",
"ZMESVIN": "SVU20220711LED2222",
"ZMESDATE": "2022-09-01",
"IDNRK": "000000010021000697",
"ZYL04": "",
"SORTF": "X",
"ZYL06": "",
"ZCZZT": "U",
"AUSCH": "",
"ZYL05": ""
}
]
},
]
}
}
}
我的解答思路和尝试过的方法
我尝试过使用网上说的建立一个空对象去实现,但是达不到想到的效果,我都json格式会被打乱。
我想要达到的结果
把两个对象里面的值都追加进对象和数组里面。