Gson: Merge Gson and JsonObject

Created on 5 Aug 2015  路  1Comment  路  Source: google/gson

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.

Most helpful comment

      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());
      }

>All comments

      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());
      }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

GoogleCodeExporter picture GoogleCodeExporter  路  14Comments

danieleguiducci picture danieleguiducci  路  34Comments

priyankajagtap18 picture priyankajagtap18  路  14Comments

GoogleCodeExporter picture GoogleCodeExporter  路  16Comments

kramer picture kramer  路  26Comments