Using gson to deserialize specific JSON field of an object

When parsing such a simple structure, no need to have dedicated classes.

Solution 1 :

To get the imgurURL from your String with gson, you can do this :

JsonParser parser = new JsonParser();
JsonObject obj = parser.parse(toExtract).getAsJsonObject();
String imgurl = obj.get("imgurl").getAsString();

This uses a raw parsing into a JsonObject.

Solution 2 :

Alternatively, you could extract your whole data in a Properties instance using

 Properties data = gson.fromJson(toExtract, Properties.class);

and read your URL with

String imgurl = data.getProperty("imgurl");

Leave a Comment