1

I have this JSON that I retrieved using Bing-Search-API. Now, I'm not sure how to access the nested elements using GSON. I already made the source files for the JSON Structure Data.

If I do this:

Gson gson = new Gson();

JsonParser parser = new JsonParser();

JsonArray Jarray = parser.parse(jsonText).getAsJsonArray();

It is going to throw me that is not a JsonArray, so If I change it to JsonObject, how can I retrieve the String MediaUrl from Results.java?

Thank you

Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69

1 Answers1

4

Based on the javadoc of Gson class:

    Gson gson = new Gson();
    Response response = gson.fromJson(jsonText, Response.class);
    Results firstResult = response.getD().getResults().get(0);
    System.out.println(firstResult.getMediaUrl());

So you don't need to use the JsonParser directly.

Your java classes have to be modified a little bit for this to work:

  • the type of results field in D.java has to be List<Results> so that Gson can find out the class of objects to populate with.
  • the naming of attributes/fields is inconsistent, some starts with lower case, others with uppercase. Make sure they are the same in the java classes and in the json string (considering case sensitivity). This issue might be addressed with using the appropriate FieldNamingStrategy for serialization/deserialization.
Katona
  • 4,816
  • 23
  • 27
  • Thank you, but it's returning null. I am using this as you mentioned: Gson gson = new Gson(); Results res = gson.fromJson(jsonText, Results.class); System.out.println( res.getMediaUrl()); – Luis Lavieri Aug 13 '13 at 16:59
  • Could you take a look to my gist please? – Luis Lavieri Aug 13 '13 at 17:02
  • [This is what I tried](https://gist.github.com/lumalav/6222895#file-jsonparser-java) – Luis Lavieri Aug 13 '13 at 17:16
  • @LuisLavieri updated my answer, your "root element" is `Response`, so that's what you have to specify at `gson.fromJson(...)` – Katona Aug 13 '13 at 17:26
  • Thanks, but how can I access MediaUrl? when I try to populate a List from "response.getD().getResults()" it throws me an IllegalException – Luis Lavieri Aug 13 '13 at 17:32
  • @LuisLavieri I updated my answer, please note that I had to modify your java classes to get it work, how did you get them? – Katona Aug 13 '13 at 18:05
  • Now it is giving me Type mismatch: cannot convert from Object to BingSearchExample.Results and the option that is giving me is adding a cast to Results which does not work or change result type. :( – Luis Lavieri Aug 13 '13 at 21:05
  • I'm sorry, I was trying to follow your example. Did you change jsonParser2.java? that one throws me a NPE – Luis Lavieri Aug 13 '13 at 21:11
  • @LuisLavieri use the code snippet in my answer for deserialization, that is use `Gson` class instead of `JsonParser` – Katona Aug 13 '13 at 21:34
  • Hi, sorry for bothering you again. But now I was trying to get MediaUrl from Thumbnail.java. I tried changing Result.java so it could return a List, and then in my code I tried in a loop thumbnails[i] = response.getD().getResults().thumbnail.get(i); , but, it is not working. What is happening? – Luis Lavieri Aug 14 '13 at 17:11
  • @LuisLavieri continuing the example in the answer: `firstResult.getThumbnail().getMediaUrl()` – Katona Aug 14 '13 at 18:23