Gson: Directly convert String to JsonObject (no POJO) Gson: Directly convert String to JsonObject (no POJO) json json

Gson: Directly convert String to JsonObject (no POJO)


use JsonParser; for example:

JsonParser parser = new JsonParser();JsonObject o = parser.parse("{\"a\": \"A\"}").getAsJsonObject();


Try to use getAsJsonObject() instead of a straight cast used in the accepted answer:

JsonObject o = new JsonParser().parse("{\"a\": \"A\"}").getAsJsonObject();


String jsonStr = "{\"a\": \"A\"}";Gson gson = new Gson();JsonElement element = gson.fromJson (jsonStr, JsonElement.class);JsonObject jsonObj = element.getAsJsonObject();