首页 > 解决方案 > 在javascript中将多个json对象合并为具有相同键的单个对象

问题描述

我知道看起来这个问题可能很简单,但我对对象在 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最后将所有 json 对象组合并存储在一个名为.tempJson2tempJson3tempJson

但是,当我尝试使用时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 的解决方案。

标签: javascripthtml

解决方案


好的,如果我的问题是正确的,您似乎想发送 3 个数据块作为 HTTP 请求的 JSON 有效负载。块是:

{

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

{
    "request": {
        "command": "close",
        "commandData":{
          }
      }
}

{
    "request": {
        "command": "status",
        "commandData":{
         }
   }
}

但这不是一个有效的 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. 或者是这些的某种组合。


推荐阅读