I've made some improvements over Gson API to iterate elements through a given
path in a groovy style
http://dl.dropbox.com/u/20272624/Pson.java
Instead of doing:
JsonElement response = APIUtils.callAPI(countryId, APIUtils.HOTELS + "/" +
hotelId + "?includeamenities=true"));
if(
!response.getAsJsonObject.get("hotels").getAsJsonArray().get(0).getAsJsonObject(
).get("geoLocation").getAsJsonObject().get("latitude").isJsonNull()) {
...
}
Do:
Pson response = new Pson(DespegarAPIUtils.callAPI(countryId,
DespegarAPIUtils.HOTELS + "/" + hotelId + "?includeamenities=true"));
if( !response.get("hotels[0].geoLocation.latitude").isJsonNull()) {
cityId = response.get("hotels[0].cityId").getAsString();
hotelLatitude = response.get("hotels[0].geoLocation.latitude").getAsDouble();
hotelLongitude = response.get("hotels[0].geoLocation.longitude").getAsDouble();
}
Also, I've made an iterator for JsonArrays:
for (Pson range : configJson.getIterable("configuration.ranges")) {
int startRange = range.get("startRange").getAsInt();
int endRange = range.get("endRange").getAsInt();
if(startRange <= rangeValue && rangeValue <= endRange) {
return range.get("value").getAsString();
}
}
instead of old coding style:
JsonArray arr =
response.getAsJsonObject().get("configuration").getAsJsonObject().get("ranges").
getAsJsonArray();
for(int i = 0; i < arr.size(); i++) {
JsonElement range = arr.get(i).getAsJsonObject();
....
}
Best regards!
Ignacio Nieto
Original issue reported on code.google.com by nietofar...@gmail.com on 16 May 2012 at 5:37
Original issue reported on code.google.com by
nietofar...@gmail.com
on 16 May 2012 at 5:37Attachments: