繁体   English   中英

使用JavaScript中的相同键将多个json对象合并为单个对象

[英]Merge multiple json objects to single object with same keys in javascript

我知道看这个问题可能很简单,但是我对对象在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":{
         }
   }
}

对于以上请求,我将tempJson1tempJson2tempJson3分为三个json对象,最后将所有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的解决方案。

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

{

    "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。 或这些的某种组合。

您不能在一个对象中一次又一次拥有相同的键。“ request”键在同一对象内三次使用。因此它将替换前两个对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM