abdulrehmanz / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

json path parsing #444

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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

Attachments:

GoogleCodeExporter commented 9 years ago
Neat. I don't think there's much action for us to take here.

Original comment by limpbizkit on 30 Jun 2012 at 3:12