首先分析下目前號稱最快的FastJson,這個是全部人都驗證過的,解析速度確實比較快,不過也須要根據數據量來看,數據量小的時候,Gson性能要稍微優於FastJson,但在數據量大解析的狀況下,FastJson的速度就要明顯快於Gson。具體緣由,我沒研究過,只是作過測試,確實是這樣。javascript
性能測試代碼以下:php
/** * 測試Bean類 */
public class TestBean { private String name; private int age; private String no; public TestBean() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int j) { this.age = j; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } }
/** * 比較FastJson和Gson的效率 */
public void comparedFastJsonAndGson() { List<TestBean> list = new ArrayList<>(); int j = 0; TestBean u = null; //數據生成 while (j < 1000000) { u = new TestBean(); u.setAge(j); u.setName("zhangsan " + j); u.setNo("" + j); list.add(u); j++; } //作測試時,兩個方法不要同時使用,註釋掉另外一個分別運行,而後再比較時間,否則結果不許 // FastJson性能測試 fastJsonTest(list); System.out.println("!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); // Gson性能測試 gsonTest(list); } /** * FastJsonTest * * @param list */ private void fastJsonTest(List<TestBean> list) { long s = System.currentTimeMillis(); System.out.println("before alibaba:" + s); String aliJson = com.alibaba.fastjson.JSON.toJSONString(list); long e = System.currentTimeMillis(); System.out.println("after alibaba:" + e); System.out.println("beanToJson:" + (e - s)); list = null; long s3 = System.currentTimeMillis(); List<TestBean> sult = JSON.parseArray(aliJson, TestBean.class); // List<U> sult = (List<U>) JSONObject.parse(aliJson); long e3 = System.currentTimeMillis(); System.out.println("JsonTobean:" + (e3 - s3)); } /** * GsonTest * * @param list */ private void gsonTest(List<TestBean> list) { long s1 = System.currentTimeMillis(); System.out.println("before Gson:" + s1); Gson gson = new Gson(); String gsonStr = gson.toJson(list); long e1 = System.currentTimeMillis(); System.out.println("after Gson:" + e1); System.out.println("beanToJson:" + (e1 - s1)); list = null; long s4 = System.currentTimeMillis(); // type 獲取List<U>類型的class Type type = new TypeToken<List<TestBean>>() { }.getType(); List<TestBean> sult2 = gson.fromJson(gsonStr, type); long e4 = System.currentTimeMillis(); System.out.println("JsonTobean:" + (e4 - s4)); }
下面介紹下兩種解析方式的具體使用方法(這裏使用的是K780數據網的5~7每天氣預報信息)html
/** * @author Jerry 2016.4.15 * */ public class Weather { private String days; // 日期 private String week; // 星期 private String citynm; // 城市/地區 private String temperature;// 溫度 private String weather; // 天氣 private String wind;// 風向 private String winp;// 風力 public Weather() { } public String getDays() { return days; } public void setDays(String days) { this.days = days; } public String getWeek() { return week; } public void setWeek(String week) { this.week = week; } public String getCitynm() { return citynm; } public void setCitynm(String citynm) { this.citynm = citynm; } public String getTemperature() { return temperature; } public void setTemperature(String temperature) { this.temperature = temperature; } 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 getWinp() { return winp; } public void setWinp(String winp) { this.winp = winp; } @Override public String toString() { return "Weather [days=" + days + ", week=" + week + ", citynm=" + citynm + ", temperature=" + temperature + ", weather=" + weather + ", wind=" + wind + ", winp=" + winp + "]"; } }
/** * @author Jerry */ public class WeatherGson { private String success; private List<Weather> result; // 此處List 名字,必須爲Json數組中鍵的名字,必須相同 public WeatherGson() { } public WeatherGson(String success, List<Weather> result) { this.success = success; this.result = result; } public String getSuccess() { return success; } public void setSuccess(String success) { this.success = success; } public List<Weather> getList() { return result; } public void setList(List<Weather> list) { this.result = list; } @Override public String toString() { return "WeatherJson [success=" + success + ", list=" + result + "]"; } }
如下因此方法都卸載JsonDemo類中java
/** * 獲取網絡Json數據String * * @param weaid * @return */ public String getJsonData() { System.out.println("請等待..."); String url = "http://api.k780.com:88/?app=weather.future&weaid=1&&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json"; //將獲取到的數據轉換成字符串,此處是我本身封裝的工具類 String jsonData = HttpUitls.doPostToString(url, "utf-8"); return jsonData; }
首先是FastJson的解析:json
/** * fastJson 解析 * * @param jsonData * @return */ public List<Weather> fastJsonParser(String jsonData) { // 獲取jsonObject對象 JSONObject object = JSON.parseObject(jsonData); String success = object.getString("success"); if ("1".equals(success)) { // 從jsonObject對象中獲取 result 對象的值(Json數組) String result = object.getString("result"); // 將Json數組轉換成List集合 List<Weather> list = JSON.parseArray(result, Weather.class); return list; } else { throw new RuntimeException("獲取信息失敗:" + success); } }
接着是Gson的解析:api
/** * Gson 解析 * * @param jsonData */ public List<Weather> gsonParser(String jsonData) { Gson gson = new Gson(); System.out.println(jsonData); // List<Weather> list2 = gson.fromJson(jsonData, new // TypeToken<List<Weather>>(){}.getType()); WeatherGson fromJson = gson.fromJson(jsonData, WeatherGson.class); if ("1".equals(fromJson.getSuccess())) { return fromJson.getList(); } else { throw new RuntimeException("獲取信息失敗:" + fromJson.getSuccess()); } }
最後是Json解析:數組
/** * Json解析 * * @param jsonData * @return */ public List<Weather> jsonParser(String jsonData) { list = new ArrayList<>(); try { org.json.JSONObject object = new org.json.JSONObject(jsonData); JSONArray result = object.getJSONArray("result"); for (int i = 0; i < result.length(); i++) { org.json.JSONObject object2 = result.getJSONObject(i); this.weather = new Weather(); String days = object2.getString("days"); String week = object2.getString("week"); String citynm = object2.getString("citynm"); String temperature = object2.getString("temperature"); String weather = object2.getString("weather"); String wind = object2.getString("wind"); String winp = object2.getString("winp"); this.weather.setDays(days); this.weather.setWeek(week); this.weather.setCitynm(citynm); this.weather.setTemperature(temperature); this.weather.setWeather(weather); this.weather.setWind(wind); this.weather.setWinp(winp); list.add(this.weather); } return list; } catch (JSONException e) { e.printStackTrace(); } return null; }
Main:網絡
public class Main { public static void main(String[] args) { JsonDemo jsonDemo = new JsonDemo(); // 比較FastJson和Gson 的效率 jsonDemo.comparedFastJsonAndGson(); // 從網絡獲取Json數據 String jsonData = jsonDemo.getJsonData(); // 使用Json獲取數據集合 List<Weather> list = jsonDemo.jsonParser(jsonData); for (Weather weather : list) { System.out.println(weather); } // 使用FastJson 獲取數據集合 List<Weather> list2 = jsonDemo.fastJsonParser(jsonData); for (Weather weather : list2) { System.out.println(weather); } // 使用Gson 獲取數據集合 List<Weather> list3 = jsonDemo.gsonParser(jsonData); for (Weather weather : list3) { System.out.println(weather); } } }from: http://www.voidcn.com/blog/ming2316780/article/p-5811077.html