簡體   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