首先分析下目前号称最快的FastJson,这个是所有人都验证过的,解析速度确实比较快,不过也需要根据数据量来看,数据量小的时候,Gson性能要稍微优于FastJson,但在数据量大解析的情况下,FastJson的速度就要明显快于Gson。具体原因,我没研究过,只是做过测试,确实是这样。

性能测试代码如下:

  1. /** * 测试Bean类 */
  2. public class TestBean {
  3. private String name;
  4. private int age;
  5. private String no;
  6.  
  7. public TestBean() {
  8. }
  9.  
  10. public String getName() {
  11. return name;
  12. }
  13.  
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17.  
  18. public int getAge() {
  19. return age;
  20. }
  21.  
  22. public void setAge(int j) {
  23. this.age = j;
  24. }
  25.  
  26. public String getNo() {
  27. return no;
  28. }
  29.  
  30. public void setNo(String no) {
  31. this.no = no;
  32. }
  33.  
  34. }
  1. /** * 比较FastJson和Gson的效率 */
  2. public void comparedFastJsonAndGson() {
  3. List<TestBean> list = new ArrayList<>();
  4. int j = 0;
  5. TestBean u = null;
  6. //数据生成
  7. while (j < 1000000) {
  8. u = new TestBean();
  9. u.setAge(j);
  10. u.setName("zhangsan " + j);
  11. u.setNo("" + j);
  12. list.add(u);
  13. j++;
  14. }
  15. //做测试时,两个方法不要同时使用,注释掉另一个分别运行,然后再比较时间,不然结果不准
  16. // FastJson性能测试
  17. fastJsonTest(list);
  18. System.out.println("!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  19. // Gson性能测试
  20. gsonTest(list);
  21. }
  22.  
  23. /** * FastJsonTest * * @param list */
  24. private void fastJsonTest(List<TestBean> list) {
  25. long s = System.currentTimeMillis();
  26. System.out.println("before alibaba:" + s);
  27. String aliJson = com.alibaba.fastjson.JSON.toJSONString(list);
  28. long e = System.currentTimeMillis();
  29. System.out.println("after alibaba:" + e);
  30. System.out.println("beanToJson:" + (e - s));
  31. list = null;
  32. long s3 = System.currentTimeMillis();
  33. List<TestBean> sult = JSON.parseArray(aliJson, TestBean.class);
  34. // List<U> sult = (List<U>) JSONObject.parse(aliJson);
  35. long e3 = System.currentTimeMillis();
  36. System.out.println("JsonTobean:" + (e3 - s3));
  37. }
  38.  
  39. /** * GsonTest * * @param list */
  40. private void gsonTest(List<TestBean> list) {
  41. long s1 = System.currentTimeMillis();
  42. System.out.println("before Gson:" + s1);
  43. Gson gson = new Gson();
  44. String gsonStr = gson.toJson(list);
  45. long e1 = System.currentTimeMillis();
  46. System.out.println("after Gson:" + e1);
  47. System.out.println("beanToJson:" + (e1 - s1));
  48. list = null;
  49. long s4 = System.currentTimeMillis();
  50. // type 获取List<U>类型的class
  51. Type type = new TypeToken<List<TestBean>>() {
  52. }.getType();
  53. List<TestBean> sult2 = gson.fromJson(gsonStr, type);
  54. long e4 = System.currentTimeMillis();
  55. System.out.println("JsonTobean:" + (e4 - s4));
  56. }

下面介绍下两种解析方式的具体使用方法(这里使用的是K780数据网的5~7天天气预报信息)

  1. /** * @author Jerry 2016.4.15 * */
  2. public class Weather {
  3. private String days; // 日期
  4. private String week; // 星期
  5. private String citynm; // 城市/地区
  6. private String temperature;// 温度
  7. private String weather; // 天气
  8. private String wind;// 风向
  9. private String winp;// 风力
  10.  
  11. public Weather() {
  12. }
  13.  
  14. public String getDays() {
  15. return days;
  16. }
  17.  
  18. public void setDays(String days) {
  19. this.days = days;
  20. }
  21.  
  22. public String getWeek() {
  23. return week;
  24. }
  25.  
  26. public void setWeek(String week) {
  27. this.week = week;
  28. }
  29.  
  30. public String getCitynm() {
  31. return citynm;
  32. }
  33.  
  34. public void setCitynm(String citynm) {
  35. this.citynm = citynm;
  36. }
  37.  
  38. public String getTemperature() {
  39. return temperature;
  40. }
  41.  
  42. public void setTemperature(String temperature) {
  43. this.temperature = temperature;
  44. }
  45.  
  46. public String getWeather() {
  47. return weather;
  48. }
  49.  
  50. public void setWeather(String weather) {
  51. this.weather = weather;
  52. }
  53.  
  54. public String getWind() {
  55. return wind;
  56. }
  57.  
  58. public void setWind(String wind) {
  59. this.wind = wind;
  60. }
  61.  
  62. public String getWinp() {
  63. return winp;
  64. }
  65.  
  66. public void setWinp(String winp) {
  67. this.winp = winp;
  68. }
  69.  
  70. @Override
  71. public String toString() {
  72. return "Weather [days=" + days + ", week=" + week + ", citynm=" + citynm + ", temperature=" + temperature
  73. + ", weather=" + weather + ", wind=" + wind + ", winp=" + winp + "]";
  74. }
  75. }
  1. /** * @author Jerry */
  2. public class WeatherGson {
  3. private String success;
  4. private List<Weather> result; // 此处List 名字,必须为Json数组中键的名字,必须相同
  5.  
  6. public WeatherGson() {
  7. }
  8.  
  9. public WeatherGson(String success, List<Weather> result) {
  10. this.success = success;
  11. this.result = result;
  12. }
  13.  
  14. public String getSuccess() {
  15. return success;
  16. }
  17.  
  18. public void setSuccess(String success) {
  19. this.success = success;
  20. }
  21.  
  22. public List<Weather> getList() {
  23. return result;
  24. }
  25.  
  26. public void setList(List<Weather> list) {
  27. this.result = list;
  28. }
  29.  
  30. @Override
  31. public String toString() {
  32. return "WeatherJson [success=" + success + ", list=" + result + "]";
  33. }
  34. }

以下所以方法都卸载JsonDemo类中

  1. /** * 获取网络Json数据String * * @param weaid * @return */
  2. public String getJsonData() {
  3. System.out.println("请等待...");
  4.  
  5. String url = "http://api.k780.com:88/?app=weather.future&weaid=1&&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json";
  6. //将获取到的数据转换成字符串,此处是我自己封装的工具类
  7. String jsonData = HttpUitls.doPostToString(url, "utf-8");
  8. return jsonData;
  9. }

首先是FastJson的解析:

  1. /** * fastJson 解析 * * @param jsonData * @return */
  2. public List<Weather> fastJsonParser(String jsonData) {
  3. // 获取jsonObject对象
  4. JSONObject object = JSON.parseObject(jsonData);
  5. String success = object.getString("success");
  6. if ("1".equals(success)) {
  7. // 从jsonObject对象中获取 result 对象的值(Json数组)
  8. String result = object.getString("result");
  9. // 将Json数组转换成List集合
  10. List<Weather> list = JSON.parseArray(result, Weather.class);
  11. return list;
  12. } else {
  13. throw new RuntimeException("获取信息失败:" + success);
  14. }
  15. }

接着是Gson的解析:

  1. /** * Gson 解析 * * @param jsonData */
  2. public List<Weather> gsonParser(String jsonData) {
  3. Gson gson = new Gson();
  4. System.out.println(jsonData);
  5. // List<Weather> list2 = gson.fromJson(jsonData, new
  6. // TypeToken<List<Weather>>(){}.getType());
  7. WeatherGson fromJson = gson.fromJson(jsonData, WeatherGson.class);
  8. if ("1".equals(fromJson.getSuccess())) {
  9. return fromJson.getList();
  10. } else {
  11. throw new RuntimeException("获取信息失败:" + fromJson.getSuccess());
  12. }
  13. }

最后是Json解析:

  1. /** * Json解析 * * @param jsonData * @return */
  2. public List<Weather> jsonParser(String jsonData) {
  3. list = new ArrayList<>();
  4. try {
  5. org.json.JSONObject object = new org.json.JSONObject(jsonData);
  6. JSONArray result = object.getJSONArray("result");
  7. for (int i = 0; i < result.length(); i++) {
  8. org.json.JSONObject object2 = result.getJSONObject(i);
  9. this.weather = new Weather();
  10. String days = object2.getString("days");
  11. String week = object2.getString("week");
  12. String citynm = object2.getString("citynm");
  13. String temperature = object2.getString("temperature");
  14. String weather = object2.getString("weather");
  15. String wind = object2.getString("wind");
  16. String winp = object2.getString("winp");
  17. this.weather.setDays(days);
  18. this.weather.setWeek(week);
  19. this.weather.setCitynm(citynm);
  20. this.weather.setTemperature(temperature);
  21. this.weather.setWeather(weather);
  22. this.weather.setWind(wind);
  23. this.weather.setWinp(winp);
  24. list.add(this.weather);
  25. }
  26. return list;
  27. } catch (JSONException e) {
  28. e.printStackTrace();
  29. }
  30. return null;
  31. }

Main:

  1. public class Main {
  2.  
  3. public static void main(String[] args) {
  4. JsonDemo jsonDemo = new JsonDemo();
  5. // 比较FastJson和Gson 的效率
  6. jsonDemo.comparedFastJsonAndGson();
  7.  
  8. // 从网络获取Json数据
  9. String jsonData = jsonDemo.getJsonData();
  10.  
  11. // 使用Json获取数据集合
  12. List<Weather> list = jsonDemo.jsonParser(jsonData);
  13. for (Weather weather : list) {
  14. System.out.println(weather);
  15. }
  16.  
  17. // 使用FastJson 获取数据集合
  18. List<Weather> list2 = jsonDemo.fastJsonParser(jsonData);
  19. for (Weather weather : list2) {
  20. System.out.println(weather);
  21. }
  22.  
  23. // 使用Gson 获取数据集合
  24. List<Weather> list3 = jsonDemo.gsonParser(jsonData);
  25. for (Weather weather : list3) {
  26. System.out.println(weather);
  27. }
  28. }
  29. }
  30.  
  31. from: http://www.voidcn.com/blog/ming2316780/article/p-5811077.html

FastJson和Gson和Json数据解析分析和用法的更多相关文章

  1. 使用Gson轻松解决复杂结构的Json数据解析

    转载请注明来源: http://blog.csdn.net/kjunchen/article/details/50961803 JSON简介 JSON(JavaScript Object Notati ...

  2. JSON数据解析(GSON方式) (转)

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种理想的数据交换格式. 在上一篇博文<Andro ...

  3. Android JSON数据解析(GSON方式)

    要创建和解析JSON数据,也可以使用GSON来完成.GSON是Google提供的用来在Java对象和JSON数据之间进行映射的Java类库.使用GSON,可以很容易的将一串JSON数据转换为一个Jav ...

  4. Android学习笔记之JSON数据解析

    转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...

  5. 最简单简洁高效的Json数据解析

    一.无图无真相 二.主要代码 1.导入jar包 拷贝fastjson.jar包到projectlibs包下 2.封装工具类JsonUtil.java package com.example.parse ...

  6. 多叉树结构:JSON数据解析(二)

    多叉树结构:JSON数据解析(二) 在上篇文章中提到了JSON数据解析的基本方法,但是方法效率太低,这里接着上篇文章写写如何利用多叉树结构,定义对象,实现JSON数据字段快速随机访问. JSON数据通 ...

  7. [开源 .NET 跨平台 数据采集 爬虫框架: DotnetSpider] [四] JSON数据解析

    [DotnetSpider 系列目录] 一.初衷与架构设计 二.基本使用 三.配置式爬虫 四.JSON数据解析与配置系统 场景模拟 假设由于漏存JD SKU对应的店铺信息.这时我们需要重新完全采集所有 ...

  8. JSON数据解析(转)

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种理想的数据交换格式. 本文将主要介绍在Android ...

  9. [开源 .NET 跨平台 Crawler 数据采集 爬虫框架: DotnetSpider] [四] JSON数据解析

    [DotnetSpider 系列目录] 一.初衷与架构设计 二.基本使用 三.配置式爬虫 四.JSON数据解析与配置系统 五.如何做全站采集 场景模拟 接上一篇, JD SKU对应的店铺信息是异步加载 ...

随机推荐

  1. Web前端开发最佳实践(11):使用更严格的JavaScript编码方式,提高代码质量

    前言 JavaScript语言由于其固有的灵活性,所以导致开发者可以写出很多诡异的代码,甚至一些较为正常的特性,如类型隐式转换.this的指代等等,也会让刚接触此语言的开发者头大不已.尤其是那些熟知其 ...

  2. js判断一个字符串是否是数字

    function isNumber(val) { var regPos = /^\d+(\.\d+)?$/; //非负浮点数 var regNeg = /^(-(([0-9]+\.[0-9]*[1-9 ...

  3. 【知了堂学习笔记】java 编写几种常见排序算法

    排序的分类: 一.交换排序 所谓交换,就是根据序列中两个记录键值的比较结果来对换这两个记录在序列中的位置,交换排序的特点是:将键值较大的记录向序列的尾部移动,键值较小的记录向序列的前部移动. 1.冒泡 ...

  4. 美团外卖iOS App冷启动治理

    一.背景 冷启动时长是App性能的重要指标,作为用户体验的第一道“门”,直接决定着用户对App的第一印象.美团外卖iOS客户端从2013年11月开始,历经几十个版本的迭代开发,产品形态不断完善,业务功 ...

  5. SpringMvc和servlet对比

    一.servlet实现登录. 咱们先来看一下servlet实现注册登录. <servlet> <servlet-name>LoginServlet</servlet-na ...

  6. 【洛谷】4317:花神的数论题【数位DP】

    P4317 花神的数论题 题目背景 众所周知,花神多年来凭借无边的神力狂虐各大 OJ.OI.CF.TC …… 当然也包括 CH 啦. 题目描述 话说花神这天又来讲课了.课后照例有超级难的神题啦…… 我 ...

  7. 重温PHP之冒泡排序

    冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法. 它重复地走访过要排序的元素列,依次比较两个相邻的元素,如果他们的顺序(如从大到小.首字母从A到Z)错误就把他们交换过来.走 ...

  8. [JAVA] JAVA 文档注释

    Java 程序设计环境 文档注释 javadoc JDK中包含的javadoc工具可以由源文件生成一个HTML文档. javadoc从以下几个特性中抽取信息 包 公有类与接口 公有的和受保护的构造器及 ...

  9. 转:在两个页面间翻转设置Animation动作的一些总结

    今天碰到两个页面之间翻转的动作设计问题,发现了一些问题,故做个总结,很多都写在注释部分: 1.首先,我们来手动创建两个view以及相应的viewController.是手动,不是用IB (1)刚开始只 ...

  10. Extjs Ajax 分页

    var storeCpye = new Ext.data.GroupingStore({ proxy : new Ext.data.HttpProxy({ url : 'cxgl_cpye.app?d ...