简体   繁体   English

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

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

I know looking this question might be simple, but I'm bit confused with the way of objects reacts in javascript. 我知道看这个问题可能很简单,但是我对对象在javascript中的反应方式有些困惑。 I tried to search as much as possible for a solution and couldn't find any. 我试图搜索尽可能多的解决方案,但找不到任何解决方案。

Here, I wanted to send the below json request. 在这里,我想发送以下json请求。 (EXPECTED OUTPUT) (预期输出)

 {

    "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":{
         }
   }
}

For the above request, I have splitted as three json objects viz tempJson1 , tempJson2 and tempJson3 and finally combine all the json objects and store in a variable called tempJson . 对于以上请求,我将tempJson1tempJson2tempJson3分为三个json对象,最后将所有json对象组合在一起并存储在名为tempJson的变量中。

But, when I try to use Object.assign() , it is taking only the tempJson3 and not merging all three json objects. 但是,当我尝试使用Object.assign() ,它仅使用tempJson3而不合并所有三个json对象。

Where am I missing? 我在哪里想念? Any help? 有什么帮助吗?

  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: Need solution with pure javascript and no ES6. PS:需要纯JavaScript且没有ES6的解决方案。

Ok, if I got the question right, it seems you want to send 3 blocks of data as JSON payload for an HTTP request. 好的,如果我的问题正确,看来您想发送3个数据块作为JSON有效负载以用于HTTP请求。 Blocks are: 块是:

{

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

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

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

But this is not a valid JSON . 但这不是有效的JSON You must have one entity (object or array in this case) as the root element. 您必须具有一个实体(在这种情况下为对象或数组)作为根元素。 So I can think of these possible solutions: 所以我可以想到以下可能的解决方案:

  1. Send 3 separated HTTP requests with one "request" JSON object in each request. 发送3个单独的HTTP请求,每个请求中带有一个“请求” JSON对象。

  2. If you need to send all three in one request, you have to group it in some way. 如果您需要在一个请求中发送所有三个请求,则必须以某种方式将其分组。 For example: 例如:

2.1. 2.1。 An array 数组

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

// so the code would be:
var tempJson = [tempJson1, tempJson2, tempJson3];

2.2. 2.2。 An object with differently named properties: 具有不同命名属性的对象:

{
  "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.3。 Or some kind of a combination of those. 或这些的某种组合。

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

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

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