如何将多个 JSON 字符串合并为一个(Java)

huangapple go评论65阅读模式
英文:

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 :

  1. 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
}
  1. Second JSON :

    {
       "effectiveDate":"2020-09-10",
       "expiryDate":"2020-09-11",
       "quantity":0,
       "couponId":0
    }
  1. 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();
    }
}

huangapple
  • 本文由 发表于 2020年9月12日 06:14:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63854960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定