Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse dynamic JSON fields with GSON?

Tags:

So I'm using GSON to parse JSON from an API and am stuck as to how to have it parse the dynamic fields in the data.

Here is an example of the JSON data returned on a query:

{  - 30655845: {     id: "30655845"     name: "testdata     description: ""     latitude: "38"     longitude: "-122"     altitude: "0"     thumbnailURL: http://someimage.com/url.jpg     distance: 9566.6344386665 } - 28688744: {     id: "28688744"     name: "testdata2"     description: ""     latitude: "38"     longitude: "-122"     altitude: "0"     thumbnailURL: http://someimage.com/url.jpg     distance: 9563.8328713012 } } 

The way I am currently handling the single static values is with a class:

import com.google.gson.annotations.SerializedName;  public class Result  { @SerializedName("id") public int id;  @SerializedName("name") public String name;  @SerializedName("description") public String description;  @SerializedName("latitude") public Double latitude;  @SerializedName("longitude") public Double longitude;  @SerializedName("altitude") public Double altitude;  @SerializedName("thumbnailURL") public String thumbnailURL;  @SerializedName("distance") public Double distance; } 

And then I can simply use GSON to parse that:

Gson gson = new Gson();  Reader reader = new InputStreamReader(source);  Result response= gson.fromJson(reader, Result.class); 

I know this works on the sub-data as I can query and get a single entry and parse that quite easily, but what about the random integer values given for each value in the array? (ie the 30655845 and 2868874)

Any help?

like image 737
Rob Riddle Avatar asked Apr 26 '11 21:04

Rob Riddle


People also ask

Does GSON offer parse () function?

The GSON JsonParser class can parse a JSON string or stream into a tree structure of Java objects. GSON also has two other parsers. The Gson JSON parser which can parse JSON into Java objects, and the JsonReader which can parse a JSON string or stream into tokens (a pull parser).

How does GSON from JSON work?

Gson can work with arbitrary Java objects including objects for which you do not have the source. For this purpose, Gson provides several built in serializers and deserializers. A serializer allows to convert a Json string to corresponding Java type. A deserializers allows to convert from Java to a JSON representation.

Is GSON better than JSON?

GSON can use the Object definition to directly create an object of the desired type. While JSONObject needs to be parsed manually.


1 Answers

According to GSON documentation you can do things like:

Type mapType = new TypeToken<Map<Integer, Result> >() {}.getType(); // define generic type Map<Integer, Result> result= gson.fromJson(new InputStreamReader(source), mapType); 

Or you can try to write custom serializer for your class.

Disclaimer: I too, have no experience with GSon but with other frameworks like Jackson.

like image 179
Sasha O Avatar answered Oct 09 '22 09:10

Sasha O