编程 内容:1 问题已关闭

已采纳 前端JavaScriptjson格式的数据拼接

  • 查看作者
  • 打赏作者
  • 赠送礼物
  • 问题遇到的现象和发生背景
    我要把下面这段json格式的数据调整一下,把ITEM这个节点;里面包含ITEMS,不是ITEMS和ITEM是同级。
    问题相关代码,请勿粘贴截图
    这是形成这个JSON格式的代码:(写的很烂)

      var REQUEST = {}, head = {}, data = {}, req = {}, item_list = {}, items_list = {}, main = {}, item = {};
            var item = []; var items = [];
     
     
            //// Head
            head.ACCOUNT = "ESB_TEST_MES",
            head.PASSWORD = "mestest@esb",
            head.CONSUMER = "SAP";
            head.COUNT = "1";
            head.TRANSACTIONID = "BAW_PLM_PLM04_20220419193456191_1269",
            head.SRVLEVEL="1";
            head.COMPANY = "BAW";
     //item
                var ZYL02 = "";
                var ZYL01 = "";
                var ZID = $(this).find("td").eq(12).text();
                var DATUV = "20220419";
                var WERKS = $(this).find("td").eq(4).text();
                var AETXT = $(this).find("td").eq(6).text();
                var AENNR = $(this).find("td").eq(5).text();
                //items
                var ZWYBS = "123";
                var ZMESVIN = $(this).find("td").eq(10).text();
                var ZMESDATE = $(this).find("td").eq(9).text();
                var IDNRK = $(this).find("td").eq(8).text();
                var ZYL04 = "";
                var SORTF = "X";
                var ZYL06 = "";
                var ZCZZT = "";
                var AUSCH = "";
                var ZYL05 = "";
                var ZYL03 = "1";
                var MATNR = $(this).find("td").eq(3).text();
     
     
                // 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;
     
     
               
                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;
     
                ////合并
                //data.DATA = Object.assign(REQUEST);
                //req.REQUEST = Object.assign(data);
     
                //追加
                item.push(item_list, main);
                items.push(items_list);
                var JsonData = JSON.stringify(req);
    下面是JSON格式的数据:
    {
        "REQUEST": {
            "HEAD": {
                "ACCOUNT": "", 
                "PASSWORD": "", 
                "CONSUMER": "SAP", 
                "COUNT": "1", 
                "TRANSACTIONID": "BAW_PLM_PLM04_20220419193456191_1269", 
                "SRVLEVEL": "1", 
                "COMPANY": "BAW"
            }, 
            "DATA": {
                "ITEM": [
                    {
                        "ZYL02": "", 
                        "ZYL01": "", 
                        "ZID": "12345671", 
                        "DATUV": "20220419", 
                        "WERKS": "1081", 
                        "AETXT": "行项目新增", 
                        "AENNR": "000000001000", 
                        "ZYL03": "1", 
                        "MATNR": "CP12"
                    }, 
                    {
                        "ITEMS": [
                            {
                                "ZWYBS": "123", 
                                "ZMESVIN": "SVU20220711LEDHEE", 
                                "ZMESDATE": "2022-08-09", 
                                "IDNRK": "NN11", 
                                "ZYL04": "", 
                                "SORTF": "X", 
                                "ZYL06": "", 
                                "ZCZZT": "", 
                                "AUSCH": "", 
                                "ZYL05": ""
                            }
                        ]
                    }
                ]
            }
        }
    }

    运行结果及报错内容
    数据拼接
    我的解答思路和尝试过的方法
    合并或者拼接
    我想要达到的结果

    整个ITME包起来 
     
    ```c#
    {
      "REQUEST": {
        "HEAD": {
          "ACCOUNT": "ESB_TEST_MES",
          "PASSWORD": "mestest@esb",
          "CONSUMER": "SAP",
          "COUNT": "1",
          "TRANSACTIONID": "BAW_MES_E123_20220419193638191_4361",
          "SRVLEVEL": "1",
          "COMPANY": "BAW"
        },
        "DATA": {
          "ITEM": [
            {
              "ZYL02": "1",
              "ZYL01": "1",
              "ZID": "12345671",
              "DATUV": "20220419",
              "WERKS": "1081",
              "AETXT": "行项目新增",
              "AENNR": "000000001000",
              "ZYL03": "1",
              "MATNR": "CP12"
              "ITEMS": [
                {
                  "ZWYBS": "123",
                  "ZMESVIN": "L2NSPGHBXLB050913",
                  "ZMESDATE": "20220622",
                  "IDNRK": "NN11",
                  "ZYL04": "1",
                  "SORTF": "X",
                  "ZYL06": "1",
                  "ZCZZT": "I",
                  "AUSCH": "1",
                  "ZYL05": "1"              
                }
              ]
            }
          ]
        }
      }
    }

      把倒数第三行用这两行替换成试试
      item_list.ITEMs=items;
      item.push(item_list);

      他太懒了,什么都没有写

    • @ 朱熬宇: 大佬牛批 666我想了好久都没搞明白
      1个月前 (08-13) 回复

    请登录之后再进行评论

    登录