代码之家  ›  专栏  ›  技术社区  ›  UI_Dev

将多个json对象合并到javascript中具有相同键的单个对象

  •  0
  • UI_Dev  · 技术社区  · 7 年前

    我知道这个问题可能很简单,但我对javascript中对象的反应方式有点困惑。我试着尽可能多地寻找解决方案,但什么都找不到。

    在这里,我想发送下面的json请求。 (预期产出)

     {
    
        "request": {
            "command": "transaction",
             "commandData":{
              "type": "sale",
              "amount" : 0.00,
              "tenderType" : "credit",
              "referenceNumber": "",
              "kiosk" :{
                  "machineName": "string",
                  "clinicId": "string"
                }
              }
          }
    }
    
    {
        "request": {
            "command": "close",
            "commanddata":{
              }
          }
    }
    
    {
        "request": {
            "command": "status",
            "commanddata":{
             }
       }
    }
    

    对于上述请求,我将其拆分为三个json对象,即 tempJson1 , tempJson2 和 tempJson3 最后合并所有json对象并存储在一个名为 tempJson .

    但是,当我尝试使用 Object.assign() ,它只使用tempJson3,而不是合并所有三个json对象。

    我在哪里失踪了?有什么帮助吗?

     var tempJson1 = {};
        tempJson1.request = {};
        
        tempJson1.request.command = "Transaction";
        tempJson1.request.commandData = {};
        tempJson1.request.commandData.type = "sale";
        tempJson1.request.commandData.amount = document.getElementById("amount").value || "";
        tempJson1.request.commandData.tendertype = document.getElementById("tendertype").value || "";
        //tempJson.request.requireToken = document.querySelector('.consent').checked;
        tempJson1.request.commandData.referenceNumber = "";
        tempJson1.request.kiosk = {};
        tempJson1.request.kiosk.machineName = document.getElementById("machineName").value || "";
        tempJson1.request.kiosk.clinicId = document.getElementById("clinicId").value || "";
    
        var tempJson2 ={}; 
        tempJson2.request = {};
        tempJson2.request.command = "close";
        tempJson2.request.commandData = {};
        
        var tempJson3 = {};
        tempJson3.request = {};
        tempJson3.request.command = "status";
        tempJson3.request.commandData = {};
    
        var tempJson = Object.assign({},tempJson1,tempJson2, tempJson3);
        //var tempJson = tempJson1.concat(tempJson2);
        console.log(tempJson);
        console.log("tempJson = " + JSON.stringify(tempJson));
    <div>
                <input type="hidden" id="amount"/>
                <input type="hidden" id="tendertype"/>
                <input type="hidden" id="machineName"/>
                <input type="hidden" id="clinicId"/>
    
            </div>

    PS:需要纯javascript解决方案,无需ES6。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Treble Snake    7 年前

    好的,如果我答对了问题,那么您似乎想要发送3个数据块作为HTTP请求的JSON有效负载。街区包括:

    {
    
        "request": {
            "command": "transaction",
             "commandData":{
                // ...
              }
          }
    }
    
    {
        "request": {
            "command": "close",
            "commandData":{
              }
          }
    }
    
    {
        "request": {
            "command": "status",
            "commandData":{
             }
       }
    }
    

    但这是 not a valid JSON 。必须有一个实体(本例中为对象或数组)作为根元素。所以我可以想出这些可能的解决方案:

    1. 发送3个单独的HTTP请求,每个请求中有一个“请求”JSON对象。

    2. 如果你需要同时发送所有三个请求,你必须以某种方式将其分组。 例如:

    2.1. 阵列

    [
      {
        "request": {
            "command": "transaction",
             "commandData":{
                // ...
              }
          }
      },
      {
        "request": {
            "command": "close",
            "commandData":{
              }
          }
      },
      {
        "request": {
            "command": "status",
            "commandData":{
             }
       }
      } 
    ]
    
    // so the code would be:
    var tempJson = [tempJson1, tempJson2, tempJson3];
    

    2.2. 具有不同名称属性的对象:

    {
      "transaction": {
        "request": {
            "command": "transaction",
             "commandData":{
                // ...
              }
          }
      },
      "close": {
        "request": {
            "command": "close",
            "commandData":{
              }
          }
      },
      "status": {
        "request": {
            "command": "status",
            "commandData":{
             }
       }
      } 
    }
    
    // so the code would be:
    var tempJson = {
      transaction: tempJson1, 
      close: tempJson2, 
      status: tempJson3
    };
    

    2.3. 或者是这些的某种组合。

        2
  •  0
  •   anuragb26    7 年前

    不能在一个对象中重复使用同一个密钥。“请求”键在同一个对象中被使用了三次。因此,它将替换前两个对象。