gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:13:15 25 4
gpt4 key购买 nike

我知道看这个问题可能很简单,但我对 JavaScript 中对象的 react 方式有点困惑。我尝试尽可能多地搜索解决方案,但没有找到任何解决方案。

在这里,我想发送以下 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 对象,即 tempJson1tempJson2tempJson3,最后将所有 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 个数据 block 作为 HTTP 请求的 JSON 负载。 block 是:

{

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

关于javascript - 在javascript中将多个json对象合并为具有相同键的单个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54629693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com