使用Java将两个JSONArray合并为单个JSONArray数组以获取重复的全部

时间:2018-07-30 13:53:23

标签: java json java-8

我有两个JSONArray,正在尝试将其合并为一个JSONArray。我在基于这两个JSONArray的通用密钥中试图合并这两个JSONArray。

请找到我下面的Java代码,这些代码试图将它们合并为一个JSONArray

int resultSize = resultJSONArray.length();
List<String> dummyList = new ArrayList<String>();
for(int i=0; i<resultSize;i++) {
    JSONObject jobj = resultJSONArray.getJSONObject(i);
    String id = jobj.optString("id");
    for(int j=0; j<imageJSONArray.length(); j++) {
        JSONObject imageObj = imageJSONArray.getJSONObject(j);
        String imageid = imageObj.optString("id");
        if( id.equalsIgnoreCase(imageid)) {
            jobj.put("imagePath", imageObj.opt("imagePath"));
        }
    }
    resultJSONArray.put(jobj);

}

合并两个jsonarray会在合并两个JSONArray之后得到重复的整体。

请找到我的第一个JSONArray:

[  
   {  
      "imagePath":"d:\\data\\Files\\Products\\product282741_picture_small.jpg",
      "id":"283425"
   },
   {  
      "imagePath":"d:\\data\\Files\\Products\\product12803_picture_small.jpg",
      "id":"13533"
   }
]

请找到我的第二个JSONArray:

[  
   {  
      "longDescription":"",
      "code":"TE-M-1",
      "description":"NICKEL DUCT AVERAGE",
      "id":"283425"
   },
   {  
      "longDescription":"",
      "code":"TE-1",
      "description":"Duct Temperature sensor",
      "id":"13533"
   }
]

请找到我最终的JSONArray :(附带重复的全部内容)

[  
   {  
      "longDescription":"",
      "code":"TE-M-1",
      "imagePath":"d:\\data\\Files\\Products\\product282741_picture_small.jpg",
      "description":"NICKEL DUCT AVERAGE",
      "id":"283425"
   },
   {  
      "longDescription":"",
      "code":"TE-1",
      "imagePath":"d:\\data\\Files\\Products\\product12803_picture_small.jpg",
      "description":"Duct Temperature sensor ",
      "id":"13533"
   },
   {  
      "longDescription":"",
      "code":"TE-M-1",
      "imagePath":"d:\\data\\Files\\Products\\product282741_picture_small.jpg",
      "description":"NICKEL DUCT AVERAGE",
      "id":"283425"
   },
   {  
      "longDescription":"",
      "code":"TE-1",
      "imagePath":"d:\\data\\Files\\Products\\product12803_picture_small.jpg",
      "description":"Duct Temperature sensor ",
      "id":"13533"
   }
]

0 个答案:

没有答案