简体   繁体   English

将多个JSONArray中的JSONObject组合为Java中的单个JSONArray

[英]Combining JSONObject from multiple JSONArray into single JSONArray in Java

Am having 3 JSONArray like below. 正在拥有3个JSONArray,如下所示。

RegionArray : RegionArray:

[  
   {  
      "catalog_product_id":620,
      "region_id":"en_US"
   },
   {  
      "catalog_product_id":40078,
      "region_id":"en_US"
   },
   {  
      "catalog_product_id":1048,
      "region_id":"en_US"
   }
]

CountryArray: CountryArray:

[  
   {  
      "catalog_product_id":620,
      "country_id":"DE"
   },
   {  
      "catalog_product_id":40078,
      "country_id":"DE"
   }
]

CompanyArray: CompanyArray:

[
    {  
          "company_id":706,
          "catalog_product_id":40078
       },
       {  
          "company_id":706,
          "catalog_product_id":1048
       }
    }
]

These 3 different JSONArray am combining into single JSONArray and getting results like below. 这3个不同的JSONArray正在合并为单个JSONArray,并得到如下结果。 This array i have made by adding all the JSONObjects into single common array. 我通过将所有JSONObjects添加到单个公共数组中来制作此数组。 JSONArray.put(regionJSONObj);JSONArray.put(countryJSONObj);JSONArray.put(companyJSONObj);

[  

    {  
      "catalog_product_id":620,
      "region_id":"en_US"
   },
   {  
      "catalog_product_id":40078,
      "region_id":"en_US"
   },
   {  
      "catalog_product_id":1048,
      "region_id":"en_US"
   },
   {  
      "catalog_product_id":620,
      "country_id":"DE"
   },
   {  
      "catalog_product_id":40078,
      "country_id":"DE"
   },
   {  
      "company_id":706,
      "catalog_product_id":40078
   },
   {  
      "company_id":706,
      "catalog_product_id":1048
   }

]

From all the 3 source JSONArray catalog_product_id key will be same in all the 3 JSONArray., so i want to build the destination JSONArray like below format. 来自所有3个源JSONArray的catalog_product_id键在所有3个JSONArray中都相同。因此,我想像以下格式构建目标JSONArray。

[
    {
         "catalog_product_id":620,
         "region_id":"en_US",
         "country_id":"DE",
    },
    {
         "catalog_product_id":40078,
         "region_id":"en_US",
         "country_id":"DE",
         "company_id":706,
    },
    {
         "catalog_product_id":1048,
         "region_id":"en_US",
         "company_id":706

    }

]

All the 3 JSONArray RegionArray , CountryArray & CompanyArray having common key catalog_product_id . 3个JSONArray RegionArrayCountryArrayCompanyArray具有公用密钥catalog_product_id so i want to match catalog_product_id with all the 3 JSONArray and get the region , country & company information and build into an single Object for that particular catalog_product_id . 所以我想将catalog_product_id与所有3个JSONArray匹配,并获取regioncountrycompany信息,并为该特定catalog_product_id构建单个对象。

Map<String, JArray> map = 
jArrayLis.stream().collect(Collectors.toMap(JArray::getCatalog_product_id, 
Function.identity()));

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

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