Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using gson to deserialize specific JSON field of an object

I have the following JSON string:

{     "ms": "images,5160.1",     "turl": "http://ts1.mm.bing.net/th?id=I4693880201938488&pid=1.1",     "height": "178",     "width": "300",     "imgurl": "http://www.attackingsoccer.com/wp-content/uploads/2011/07/World-Cup-2012-Draw.jpg",     "offset": "0",     "t": "World Cup 2014 Qualification – Europe Draw World Cup 2012 Draw ...",     "w": "719",     "h": "427",     "ff": "jpeg",     "fs": "52",     "durl": "www.attackingsoccer.com/2011/07/world-cup-2012-qualification-europe...",     "surl": "http://www.attackingsoccer.com/2011/07/world-cup-2012-qualification-europe-draw/world-cup-2012-draw/",     "mid": "D9E91A0BA6F9E4C65C82452E2A5604BAC8744F1B",     "k": "6",     "ns": "API.images" } 

I need to store the value of imgurl in a separate string.

This is what I have till now, but this just gives me the whole JSON string instead of the specific imgurl field.

Gson gson = new Gson(); Data data = new Data(); data = gson.fromJson(toExtract, Data.class); System.out.println(data); 

toExtract is the JSON string. Here is my data class:

public class Data  {     public List<urlString> myurls; }  class urlString {     String imgurl; } 
like image 736
Sid Avatar asked Jul 11 '12 08:07

Sid


People also ask

How do I deserialize JSON with Gson?

Deserialization – Read JSON using Gson. Deserialization in the context of Gson means converting a JSON string to an equivalent Java object. In order to do the deserialization, we need a Gson object and call the function fromJson() and pass two parameters i.e. JSON string and expected java type after parsing is finished ...

Does Gson ignore extra fields?

3. Deserialize JSON With Extra Unknown Fields to Object. As you can see, Gson will ignore the unknown fields and simply match the fields that it's able to.

What does Gson toJson do?

Introduction. Gson is the main actor class of Google Gson library. It provides functionalities to convert Java objects to matching JSON constructs and vice versa. Gson is first constructed using GsonBuilder and then toJson(Object) or fromJson(String, Class) methods are used to read/write JSON constructs.

What is Gson serialization and Deserialization?

Gson can serialize a collection of arbitrary objects but can't deserialize the data without additional information. That's because there's no way for the user to indicate the type of the resulting object. Instead, while deserializing, the Collection must be of a specific, generic type.


1 Answers

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"); 
like image 136
Denys Séguret Avatar answered Oct 18 '22 17:10

Denys Séguret