Gson用法一
来源:互联网 发布:海地为什么这么穷 知乎 编辑:程序博客网 时间:2024/06/09 19:03
写项目的时候,对于json解析和生成是很繁琐的,特别是数据比较多的时候,现在普遍都是用Gson和fastJson解析,下面就介绍一下Gson的用法:
之前我也写过一点点关于gson的使用方法:http://blog.csdn.net/biao_suki/article/details/47682165
但是之前这样gson解析也是很麻烦的,主要是写实体类的时候很麻烦,所以我推荐用GsonFormat这个插件来生成实体类,非常快捷方便的一个插件
有了这个插件我就不介绍,怎么写实体类了,就说一下Gson的简单用法
在网上拿了一段json数据,就用它来做测试
{ "error": 0, "status": "success", "date": "2014-05-10", "results": [ { "currentCity": "南京", "weather_data": [ { "date": "周六(今天, 实时:19℃)", "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/dayu.png", "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/dayu.png", "weather": "大雨", "wind": "东南风5-6级", "temperature": "18℃" }, { "date": "周日", "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/zhenyu.png", "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/duoyun.png", "weather": "阵雨转多云", "wind": "西北风4-5级", "temperature": "21 ~ 14℃" } ] } ]}
根据这个json数据就用上面提到的GsonFormat插件来写实体类
public class Weather { /** * error : 0 * status : success * date : 2014-05-10 * results : [{"currentCity":"南京","weather_data":[{"date":"周六(今天, 实时:19℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/dayu.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/dayu.png","weather":"大雨","wind":"东南风5-6级","temperature":"18℃"},{"date":"周日","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/zhenyu.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"阵雨转多云","wind":"西北风4-5级","temperature":"21 ~ 14℃"}]}] */ private int error; private String status; private String date; /** * currentCity : 南京 * weather_data : [{"date":"周六(今天, 实时:19℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/dayu.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/dayu.png","weather":"大雨","wind":"东南风5-6级","temperature":"18℃"},{"date":"周日","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/zhenyu.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"阵雨转多云","wind":"西北风4-5级","temperature":"21 ~ 14℃"}] */ private List<ResultsBean> results; public int getError() { return error; } public void setError(int error) { this.error = error; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public List<ResultsBean> getResults() { return results; } public void setResults(List<ResultsBean> results) { this.results = results; } public static class ResultsBean { private String currentCity; /** * date : 周六(今天, 实时:19℃) * dayPictureUrl : http://api.map.baidu.com/images/weather/day/dayu.png * nightPictureUrl : http://api.map.baidu.com/images/weather/night/dayu.png * weather : 大雨 * wind : 东南风5-6级 * temperature : 18℃ */ private List<WeatherDataBean> weather_data; public String getCurrentCity() { return currentCity; } public void setCurrentCity(String currentCity) { this.currentCity = currentCity; } public List<WeatherDataBean> getWeather_data() { return weather_data; } public void setWeather_data(List<WeatherDataBean> weather_data) { this.weather_data = weather_data; } public static class WeatherDataBean { private String date; private String dayPictureUrl; private String nightPictureUrl; private String weather; private String wind; private String temperature; public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getDayPictureUrl() { return dayPictureUrl; } public void setDayPictureUrl(String dayPictureUrl) { this.dayPictureUrl = dayPictureUrl; } public String getNightPictureUrl() { return nightPictureUrl; } public void setNightPictureUrl(String nightPictureUrl) { this.nightPictureUrl = nightPictureUrl; } public String getWeather() { return weather; } public void setWeather(String weather) { this.weather = weather; } public String getWind() { return wind; } public void setWind(String wind) { this.wind = wind; } public String getTemperature() { return temperature; } public void setTemperature(String temperature) { this.temperature = temperature; } @Override public String toString() { return "WeatherDataBean{" + "date='" + date + '\'' + ", dayPictureUrl='" + dayPictureUrl + '\'' + ", nightPictureUrl='" + nightPictureUrl + '\'' + ", weather='" + weather + '\'' + ", wind='" + wind + '\'' + ", temperature='" + temperature + '\'' + '}'; } } @Override public String toString() { return "ResultsBean{" + "currentCity='" + currentCity + '\'' + ", weather_data=" + weather_data + '}'; } } @Override public String toString() { return "Weather{" + "error=" + error + ", status='" + status + '\'' + ", date='" + date + '\'' + ", results=" + results + '}'; }}
1、json数据转换成泛型
Gson gson = new Gson();Type type = new TypeToken<Weather>() {}.getType();Weather weather = gson.fromJson(str, type);
2、泛型转换成Json数据
Gson gson = new Gson();String str = gson.toJson(weather);
有了GsonFormat,解析json或者生成json都非常简单,基本上能解析大部分的Json数据
DEMO
0 0
- Gson用法一
- Gson用法
- Gson使用一(Gson)
- Gson的简要用法
- Gson的基本用法
- Android Gson用法
- Android Gson用法
- gson常用用法小结
- Gson的简单用法
- Gson的用法
- Gson用法1
- gson常用用法小结
- Gson用法详解
- GSON的用法
- Gson用法整理
- Gson 常用用法
- Gson用法整理
- Gson基础用法总结
- ThreadLocal源码分析
- Java中Filter、Servlet、Listener的学习
- 服务器维护系列——VNC没有反应了怎么办?
- asp.net用npoi读取ecxel数据到oracle表
- AOP实现原理
- Gson用法一
- 设计模式:装饰模式(Decorator)
- 贪心算法B(1001)
- Android--android:descendantFocusability用法简析
- 机器学习中的数学(5)-强大的矩阵奇异值分解(SVD)及其应用
- [SIMD]单指令多数据指令集(二)—— SIMD指令集在非对称算法中的应用
- 对象转型实例扩展
- javascript 跨域请求
- 对日的编码注意事项