离上一篇文章过去才4、5天,我们赶紧趁热打铁继续完成该系列的天气软件的开发。承接上一章的内容使用Volley实现网络的通信,返回给我们的是这一串Json数据{"weatherinfo":{"city":"杭州","cityid":"101210101","temp1":"1℃","temp2":"10℃","weather":"多云转晴","img1":"n1.gif","img2":"d0.gif","ptime":"18:00"}},不知有没有同学跟着我的步骤已经得到了以上的Json数据呢,接下来我们需要在我们的Android对以上数据解析!Lets go!
Json是一种类似于XML的通用数据交换格式,具有比XML更高的传输效率,体积较小,在网络传输时也可以更节省流量。但缺点也有,相比于XML语义性更差,看起来远不如XML直观。
从结构上看,所有的数据(data)最终都可以分解成三种类型,但现在基本上常用的就是映射(mapping)这种类型,一个名/值对(Name/value),即数据有一个名称,还有一个与之相对应的值,这又称作散列(hash)或字典(dictionary),比如"首都:北京"。它的规格呢也是非常简单固定的。
(1) 并列的数据之间用逗号(",")分隔,如"city":"杭州","cityid":"101210101",city与cityid两个数据之间是用,隔开的
(2) 映射用冒号(":")表示。如"city":"杭州"
(3) 并列数据的集合(数组)用方括号("[]")表示。比如如果返回的数据是有好几天的,那么天气的数据就会有好几组,会返回类似以下的数据形式"weatherinfo":[{"city":"杭州","cityid":"101210101","temp1":"1℃","temp2":"10℃","weather":"多云转晴","img1":"n1.gif","img2":"d0.gif","ptime":"18:00"},{"city":"杭 州","cityid":"101210101","temp1":"1℃","temp2":"10℃","weather":"多云转晴","img1":"n1.gif","img2":"d0.gif","ptime":"18:00"}]
(4) 映射的集合(对象)用大括号("{}")表示。例如中国天气网给我们返回的首先是一整个是Weather对象,然后里头包含一个Weatherinfo对象。
/** * 解析服务器返回的JSON数据,并将解析出的数据存储到本地。 */ public static void handleWeatherResponse(Context context, String response) { try { JSONObject jsonObject = new JSONObject(response); JSONObject weatherInfo = jsonObject.getJSONObject("weatherinfo"); String cityName = weatherInfo.getString("city"); String weatherCode = weatherInfo.getString("cityid"); String temp1 = weatherInfo.getString("temp1"); String temp2 = weatherInfo.getString("temp2"); String weatherDesp = weatherInfo.getString("weather"); String publishTime = weatherInfo.getString("ptime"); saveWeatherInfo(context, cityName, weatherCode, temp1, temp2, weatherDesp, publishTime); } catch (JSONException e) { e.printStackTrace(); } }
然后呢我们再看一下我们要解析的Json数据格式{"weatherinfo":{"city":"杭州","cityid":"101210101","temp1":"1℃","temp2":"10℃","weather":"多云转晴","img1":"n1.gif","img2":"d0.gif","ptime":"18:00"}},根据其格式我们先定义一个weather类,
package com.melhc.model; public class Weather { private Weatherinfo weatherinfo; public Weatherinfo getWeatherinfo() { return weatherinfo; } public void setWeatherInfo(Weatherinfo weatherinfo) { this.weatherinfo = weatherinfo; } }
然后这个weather类里有一个weatherinfo对象,这个对象呢又包含着city,cityid,temp1等等的对象,该怎么办呢!
package com.melhc.model; public class Weatherinfo { private String city; private String cityid; private String temp1; private String temp2; private String weather; private String ptime; public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getCityid() { return cityid; } public void setCityid(String cityid) { this.cityid = cityid; } public String getTemp1() { return temp1; } public void setTemp1(String temp1) { this.temp1 = temp1; } public String getTemp2() { return temp2; } public void setTemp2(String temp2) { this.temp2 = temp2; } public String getWeather() { return weather; } public void setWeather(String weather) { this.weather = weather; } public String getPtime() { return ptime; } public void setPtime(String ptime) { this.ptime = ptime; } @Override public String toString() { return "WeatherInfo [city=" + city + ", cityid=" + cityid + ", temp1=" + temp1 + ", temp2=" + temp2 + ", weather=" + weather + ", ptime=" + ptime + "]"; } }
public static void handleWeatherResponse(Context context, String response) { try { Gson gson = new Gson(); Weather weather = gson.fromJson(response, Weather.class); Weatherinfo info = weather.getWeatherinfo(); saveWeatherInfo(context, info); } catch (Exception e) { // TODO: handle exception } }看到没有只需要三步就完成了数据的解析,有没有很简单呢!我们只需要通过gson,from()方法就可以把数据内容映射到指定类中!SO,easy! 大家可能注意到在这三步骤结束之后还有一个saveWeatherinfo方法,这个方法用来干嘛的呢!
public static void saveWeatherInfo(Context context, Weatherinfo info) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy年M月d日", Locale.CHINA); SharedPreferences.Editor editor = PreferenceManager .getDefaultSharedPreferences(context).edit(); editor.putBoolean("city_selected", true); editor.putString("city_name", info.getCity()); editor.putString("weather_code", info.getCityid()); editor.putString("temp1", info.getTemp1()); editor.putString("temp2", info.getTemp2()); editor.putString("weather_desp", info.getWeather()); editor.putString("publish_time", info.getPtime()); LogUtil.i("UTILITY", "----------------->" + sdf.format(new Date())); editor.putString("current_date", sdf.format(new Date())); editor.commit(); }
好的,这一节课的内容就讲到这里,也希望大家能继续支持该系列的博文,你们的支持是我写下去的最大动力!今天的GSON解析数据的内容就到此结束,下一篇博文也会很快跟大家见面的。