自在猫先生 2022-08-10 17:37 采纳率: 62.9%
浏览 70
已结题

前端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"              
            }
          ]
        }
      ]
    }
  }
}

  • 写回答

2条回答 默认 最新

  • 张一毛 2022-08-10 17:58
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月18日
  • 已采纳回答 8月10日
  • 创建了问题 8月10日

悬赏问题

  • ¥20 双非跨考工科哪个专业和方向就业前景好?
  • ¥20 求会6sv辐射传输模型,辅导(可py6s🙏🏻有偿
  • ¥15 .xla后缀的文件拖到excel里什么内容也没有怎么办
  • ¥20 Workbench中Mechanical打不开、闪退是什么原因?
  • ¥240 MapReduce应用实践 学生课程
  • ¥15 hlss视频显示AUTHORITY_INVALID
  • ¥15 MAX9296A+MAX96717,美信gmsl解串有人做过吗?
  • ¥15 求帮我解决一下inode 爆满的问题(有偿)
  • ¥15 关于#vscode#的问题:布料滤波算法中C++实现pcl在Vscode中pcl库没有#include <pcl>
  • ¥15 fpga:ov5640采集tft显示