英文:
How do you combine multiple JSON strings into one ( Java )
问题
{
"businessUnitHierarchies":[
{
"actionType":"sample123",
"businessUnitHierarchy":{
"businessUnit":"sample123"
}
}
],
"description":{
"EN":"description sample",
"FR":"sample de description"
},
"name":{
"EN":"Coupon by a bot",
"FR":"Coupon par un bot"
},
"discountType":"Cost+",
"quantity":0,
"usageType":"shared",
"notes":"sample notes",
"discounts":[
{
"discountLevel":"SAMPLE",
"discountAmount":"10"
}
],
"couponId":0,
"effectiveDate":"2020-09-10",
"expiryDate":"2020-09-11",
"quantity":0,
"productHierarchies":[
{
"productHierarchy":{
"level":7
},
"businessUnit":"fgl",
"actionType":"include",
"brand":"SAMPLE",
"discountAmount":"35"
}
]
}
英文:
I tried searching for a JAVA library that I could use but to no avail.
Is there a gson/jackson/groovy library I could use to combine or merge together multiple JSON Strings into one payload?
Example :
JSON payload A, B and C
I would like both B and C to be added/merged to A.
Also removing any duplicated keys that are null or empty.
Example :
- First JSON :
{
"businessUnitHierarchies":[
{
"actionType":"sample123",
"businessUnitHierarchy":{
"businessUnit":"sample123"
}
}
],
"description":{
"EN":"description sample",
"FR":"sample de description"
},
"name":{
"EN":"Coupon by a bot",
"FR":"Coupon par un bot"
},
"discountType":"Cost+",
"quantity":0,
"usageType":"shared",
"notes":"sample notes",
"discounts":[
{
"discountLevel":"SAMPLE",
"discountAmount":"10"
}
],
"couponId":0
}
- Second JSON :
{
"effectiveDate":"2020-09-10",
"expiryDate":"2020-09-11",
"quantity":0,
"couponId":0
}
- Third JSON
{
"productHierarchies":[
{
"productHierarchy":{
"level":7
},
"businessUnit":"fgl",
"actionType":"include",
"brand":"SAMPLE",
"discountAmount":"35"
}
],
"quantity":0,
"couponId":0
}
My desired output is :
Desired Output :
{
"businessUnitHierarchies":[
{
"actionType":"sample123",
"businessUnitHierarchy":{
"businessUnit":"sample123"
}
}
],
"description":{
"EN":"description sample",
"FR":"sample de description"
},
"name":{
"EN":"Coupon by a bot",
"FR":"Coupon par un bot"
},
"discountType":"Cost+",
"quantity":0,
"usageType":"shared",
"notes":"sample notes",
"discounts":[
{
"discountLevel":"SAMPLE",
"discountAmount":"10"
}
],
"couponId":0,
"effectiveDate":"2020-09-10",
"expiryDate":"2020-09-11",
"quantity":0,
"productHierarchies":[
{
"productHierarchy":{
"level":7
},
"businessUnit":"fgl",
"actionType":"include",
"brand":"SAMPLE",
"discountAmount":"35"
}
]
}
答案1
得分: 2
这难道不正是你想要的吗?基于Gson。
void merge(JsonObject dest, JsonObject src) {
for (var entry : src.entrySet()) {
dest.add(entry.getKey(), entry.getValue());
}
}
英文:
Wouldn't this be all you want? Based on Gson.
void merge(JsonObject dest, JsonObject src) {
for (var entry : src.entrySet()) {
dest.add(entry.getKey(), entry.getValue();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论