What is the best way to combine (merge) 2 JSONObjects?

后端 未结 5 1402
梦毁少年i
梦毁少年i 2021-02-19 02:14

What is the best way to combine (merge) two JSONObjects?

JSONObject o1 = {
    \"one\": \"1\",
    \"two\": \"2\",
    \"three\": \"3\"
}
JSONObject         


        
5条回答
  •  遇见更好的自我
    2021-02-19 03:06

    Merge JsonObject(gson)-

    JsonObject data = new JsonObject();
    data = receivedJsoData.get("details").getAsJsonObject();
    
    JsonObject data2 = new JsonObject();
    data2 = receivedJsoData1.get("details").getAsJsonObject();
    
    JsonObject mergedData = new JsonObject();
    
    Set> entries = data1.entrySet();  //will return members of your object
    for (Map.Entry entry : entries) {
        mergedData.add(entry.getKey(), entry.getValue());
    }
    Set> entries1 = data2.entrySet();  //will return members of your object
    for (Map.Entry entry : entries1) {
        mergedData.add(entry.getKey(), entry.getValue());
    }
    

提交回复
热议问题