36

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;
}
richsage
  • 26,912
  • 8
  • 58
  • 65
Sid
  • 1,239
  • 2
  • 13
  • 36
  • See also [Get nested JSON object with GSON](https://stackoverflow.com/questions/23070298/get-nested-json-object-with-gson-using-retrofit) which better answers question's title. – Vadzim Jun 07 '19 at 10:38

1 Answers1

69

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");
K Erlandsson
  • 13,408
  • 6
  • 51
  • 67
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 2
    Thank You! This really helped! Could you also please explain to me why my code didn't work? I saw a few other examples where making dedicated classes also worked. – Sid Jul 11 '12 at 09:27
  • 1
    That's because the json structure doesn't correspond to an object containing a list of strings. It would have been like `{["a", "anotherurl","ttt"]}`. – Denys Séguret Jul 11 '12 at 09:40
  • 5
    Instead of toString() it is better to use getAsString() otherwise it will contain extra quotes. – Tomáš Linhart Apr 20 '15 at 16:21
  • @TomášLinhart [You're perfectly right](http://code.google.com/p/google-gson/source/browse/trunk/gson/src/main/java/com/google/gson/JsonElement.java#319), I fix that – Denys Séguret Apr 20 '15 at 16:27
  • Thanks! The second solution does not work for me, but the first one does it perfect ! – Alireza Fattahi Oct 31 '16 at 09:26
  • This solution uses Google Gson library. For those that are using Jackson library, the solution is: final String json = "{\"contentType\": \"foo\", \"fooField1\": ... }"; final ObjectNode node = new ObjectMapper().readValue(json, ObjectNode.class); if (node.has("contentType")) { System.out.println("contentType: " + node.get("contentType")); } Refer to: http://stackoverflow.com/questions/26190851/get-single-field-from-json-using-jackson – Bruno Freitas Dec 16 '16 at 18:19
  • As @AlirezaFattahi said, the second solution did not worked for me either. – Hinotori Dec 06 '19 at 15:57