Sunday, December 15, 2013

JSON Weather Parser

JSON Weather Parser

Once we have created our model we have to parse it. We can create a specific class that handles this task. First we have to create the “root” object that receive as input the entire string containing all the JSON response:
1// We create out JSONObject from the data
2JSONObject jObj = new JSONObject(data);
Then we start parsing each piece of the response:
01// We start extracting the info
02Location loc = new Location();
03 
04JSONObject coordObj = getObject("coord", jObj);
05loc.setLatitude(getFloat("lat", coordObj));
06loc.setLongitude(getFloat("lon", coordObj));
07 
08JSONObject sysObj = getObject("sys", jObj);
09loc.setCountry(getString("country", sysObj));
10loc.setSunrise(getInt("sunrise", sysObj));
11loc.setSunset(getInt("sunset", sysObj));
12loc.setCity(getString("name", jObj));
13weather.location = loc;
In the line 4,8 we create two “sub” object (coordObj and sysObj) having as parent the jObj as it clear from the JSON response. As we can see we use some helper methods to get String,int and float values:
01private static JSONObject getObject(String tagName, JSONObject jObj)  throws JSONException {
02    JSONObject subObj = jObj.getJSONObject(tagName);
03    return subObj;
04}
05 
06private static String getString(String tagName, JSONObject jObj) throws JSONException {
07    return jObj.getString(tagName);
08}
09 
10private static float  getFloat(String tagName, JSONObject jObj) throws JSONException {
11    return (float) jObj.getDouble(tagName);
12}
13 
14private static int  getInt(String tagName, JSONObject jObj) throws JSONException {
15    return jObj.getInt(tagName);
16}
And then we finally parse the weather information. We have to remember that weather tag is an array so we have to handle it differently
01// We get weather info (This is an array)
02JSONArray jArr = jObj.getJSONArray("weather");
03 
04// We use only the first value
05JSONObject JSONWeather = jArr.getJSONObject(0);
06weather.currentCondition.setWeatherId(getInt("id", JSONWeather));
07weather.currentCondition.setDescr(getString("description", JSONWeather));
08weather.currentCondition.setCondition(getString("main", JSONWeather));
09weather.currentCondition.setIcon(getString("icon", JSONWeather));
10 
11JSONObject mainObj = getObject("main", jObj);
12weather.currentCondition.setHumidity(getInt("humidity", mainObj));
13weather.currentCondition.setPressure(getInt("pressure", mainObj));
14weather.temperature.setMaxTemp(getFloat("temp_max", mainObj));
15weather.temperature.setMinTemp(getFloat("temp_min", mainObj));
16weather.temperature.setTemp(getFloat("temp", mainObj));
17 
18// Wind
19JSONObject wObj = getObject("wind", jObj);
20weather.wind.setSpeed(getFloat("speed", wObj));
21weather.wind.setDeg(getFloat("deg", wObj));
22 
23// Clouds
24JSONObject cObj = getObject("clouds", jObj);
25weather.clouds.setPerc(getInt("all", cObj));
At the end we have our Weather class filled with the data retrieved.

No comments:

Post a Comment