Reputation: 1201
Can't figure out how to parse the following JSON string with GSON into any proper object to retrieve its data. The string is:
{"Values":[{"Date":"2014-10-01","Value":386788.0},{"Date":"2014-11-01","Value":429131.0},{"Date":"2014-12-01","Value":215217.0},{"Date":"2015-01-01","Value":270422.0},{"Date":"2015-02-01","Value":261412.0},{"Date":"2015-03-01","Value":354668.0}]}
I figured out that square brackets mean that it's an ArrayList, but then there's another object inside.
If I try and try to iterare over the ArrayList:
Map<String,ArrayList<String>> map = gson.fromJson(json, Map.class);
Then it complains about:
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to java.lang.String
And if I try the opposite:
Map<String,LinkedTreeMap<String,String>> map = gson.fromJson(json, Map.class);
Then it's also complaining about it:
java.util.ArrayList cannot be cast to com.google.gson.internal.LinkedTreeMap
Don't understand that format :-/
Thanks for any advice!
Upvotes: 0
Views: 1600
Reputation: 3569
JsonParser parser = new JsonParser();
JsonObject object = (JsonObject)parser.parse(yourString);
for (Map.Entry<String,JsonElement> entry : object.entrySet()) {
JsonArray array = entry.getValue().getAsJsonArray();
for (JsonElement elementJSON : array) {
[...]
}
}
Upvotes: 1