I have Gson and JsonObject, such as:
Gson gson = new Gson();
String deviceJson = gson.toJson(device); // device is a entity
deviceJson: {
"id": 1,
"name":"JP-1"
}
JsonObject json = new JsonObject();
json.addProperty("oid", 12);
json: {
"oid":12
}
I want to merge gson and json object together, result like this:
newJson: {
"id": 1,
"name":"JP-1",
"oid":12
}
Does any way in Gson do this? Thanks.
Sorry for my English.
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("old", 12);
Object device = new Object();
JsonObject deviceJson = new Gson().toJsonTree(device).getAsJsonObject();
for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
deviceJson.add(entry.getKey(), entry.getValue());
}
Most helpful comment