如何让两个JSON合并?

yftpprvb  于 2022-11-26  发布在  其他
关注(0)|答案(1)|浏览(174)
"workItemData": "{\"pendingAppointment\":{\"orderId\":\"4000061392\",\"shipmentId\":\"SI139193599\",\"stopId\":\"1-70285467558\",\"orderStatus\":\"In-transit\",\"shipmentExecutionStatus\":\"EXECUTION_PENDING\",\"shipmentCapabilityType\":\"CAP_INTMDL STND\",\"lineOfBusiness\":\"IM\",\"stopType\":\"D\",\"locationId\":\"2856142\",\"locationName\":\"AMAZON.COM INC FC LAS7\",\"appointmentActionTraceLog\":\"OrderId:4000061392 OrderStopToken:1-70285467558 StopType:D CapabilityType:CAP_INTMDL STND. Shipment:[SI139193599]EXECUTION_PENDING;LastRailLegShipment:[SI139193598]EXECUTION_PENDING;CrossTownShipment:[SI139193597]EXECUTION_PENDING;OriginDrayShipment:[SI139193595]EXECUTION_DEPARTED LAST STOP;PickUpApptConfirmed:true. Expected ground time less than threshold of -1 hours. CurrentTime=2022-11-04T23:25:57.743-07:00[US/Pacific] Expected GroundTime=2022-10-15T10:00-07:00.Hours \"openAtStartOfDay\":true,\"closeAtEndOfDay\":true,\"isWorkOn\":true},{\"activityName\":\"RECEIVING FREIGHT\",\"dayOfWeek\":\"SUNDAY\",\"startTime\":\"00:01:00\",\"endTime\":\"23:59:00\",\"openAllDay\":true,\"openAtStartOfDay\":true,\"closeAtEndOfDay\":true,\"isWorkOn\":true},{\"activityName\":\"LOADING FREIGHT\",\"dayOfWeek\":\"SUNDAY\",\"startTime\":\"00:01:00\",\"endTime\":\"23:59:00\",\"openAllDay\":true,\"openAtStartOfDay\":true,\"closeAtEndOfDay\":true,\"isWorkOn\":true}]}},\"UIResponse\":{ \"RequestedStartDTTM\":\"2022-10-02\",\" Override24HrsSuggestedDTTM\":\"null\",\"Stop Action\":\"Drop Loaded Trailer\",\"ExceptionRequested\":\"null\"}"
}

下面是我的JSON得到的响应
我想做,\"UIResponse\":{ \"RequestedStartDTTM\":\"2022-10-02\",\" Override24HrsSuggestedDTTM\":\"null\",\"Stop Action\":\"Drop Loaded Trailer\",\"ExceptionRequested\":\"null\"}"
在括号}]}}内,并使其看起来像

"workItemData": "{\"pendingAppointment\":{\"orderId\":\"4000061392\",\"shipmentId\":\"SI139193599\",\"stopId\":\"1-70285467558\",\"orderStatus\":\"In-transit\",\"shipmentExecutionStatus\":\"EXECUTION_PENDING\",\"shipmentCapabilityType\":\"CAP_INTMDL STND\",\"lineOfBusiness\":\"IM\",\"stopType\":\"D\",\"locationId\":\"2856142\",\"locationName\":\"AMAZON.COM INC FC LAS7\",\"appointmentActionTraceLog\":\"OrderId:4000061392 OrderStopToken:1-70285467558 StopType:D CapabilityType:CAP_INTMDL STND. Shipment:[SI139193599]EXECUTION_PENDING;LastRailLegShipment:[SI139193598]EXECUTION_PENDING;CrossTownShipment:[SI139193597]EXECUTION_PENDING;OriginDrayShipment:[SI139193595]EXECUTION_DEPARTED LAST STOP;PickUpApptConfirmed:true. Expected ground time less than threshold of -1 hours. CurrentTime=2022-11-04T23:25:57.743-07:00[US/Pacific] Expected GroundTime=2022-10-15T10:00-07:00.Hours \"openAtStartOfDay\":true,\"closeAtEndOfDay\":true,\"isWorkOn\":true},{\"activityName\":\"RECEIVING FREIGHT\",\"dayOfWeek\":\"SUNDAY\",\"startTime\":\"00:01:00\",\"endTime\":\"23:59:00\",\"openAllDay\":true,\"openAtStartOfDay\":true,\"closeAtEndOfDay\":true,\"isWorkOn\":true},{\"activityName\":\"LOADING FREIGHT\",\"dayOfWeek\":\"SUNDAY\",\"startTime\":\"00:01:00\",\"endTime\":\"23:59:00\",\"openAllDay\":true,\"openAtStartOfDay\":true,\"closeAtEndOfDay\":true,\"isWorkOn\":true,\"UIResponse\":{ \"RequestedStartDTTM\":\"2022-10-02\",\" Override24HrsSuggestedDTTM\":\"null\",\"Stop Action\":\"Drop Loaded Trailer\",\"ExceptionRequested\":\"null\"}}]}}"
}

我为上面的代码编写的是

var str=",\""+"UIResponse"+"\"" +":"  +"{ "+"\""+"RequestedStartDTTM"+"\"" +":\""+this.form41.value.overReq + "\","
    +"\""+" Override24HrsSuggestedDTTM"+"\"" +":\""+this.form41.value.oversug + "\","
    +"\""+"Stop Action"+"\"" +":\""+this.form41.value.stopAction1 + "\","
    +"\""+"ExceptionRequested"+"\"" +":\""+this.form41.value.excepReqs + "\"}" ;
    let workData=this.users['workItemData'] + str;
    workData.slice(0,workData.lastIndexOf("}"))+str + "}\"" ;

我哪里错了?
我为上面的代码编写的是

var str=",\""+"UIResponse"+"\"" +":"  +"{ "+"\""+"RequestedStartDTTM"+"\"" +":\""+this.form41.value.overReq + "\","
    +"\""+" Override24HrsSuggestedDTTM"+"\"" +":\""+this.form41.value.oversug + "\","
    +"\""+"Stop Action"+"\"" +":\""+this.form41.value.stopAction1 + "\","
    +"\""+"ExceptionRequested"+"\"" +":\""+this.form41.value.excepReqs + "\"}" ;
    let workData=this.users['workItemData'] + str;
    workData.slice(0,workData.lastIndexOf("}"))+str + "}\"" ;
hwazgwia

hwazgwia1#

我希望我理解正确...您想要一个JSON/String对象吗?您可以轻松地将JSON转换为字符串,然后再转换回来,如下所示:

const json = {something: [{price: 10.5}, {price: 15.5}], value: "test"};

const stringJson = JSON.stringify(json);
console.log(stringJson);

const backToJson = JSON.parse(stringJson);
console.log(backToJson);

因此,您“构建”响应的部分可以非常容易。
如果要合并两个JSON对象,可以按如下方式操作:

jsonOne = {test: "Hello", oldProperty: "Hello Old"};
jsonTwo = {test: "Hello 2", newProperty: "Hello New"};
{...jsonOne, ...jsonTwo}

第二个参数会覆写jsonOne的特性。如果jsonOne中没有特性,则会建立该特性。范例的结果为:{test: "Hello 2", oldProperty: "Hello old", newProperty: "Hello New"}
你好,弗洛

相关问题