首先分析下目前号称最快的FastJson,这个是所有人都验证过的,解析速度确实比较快,不过也需要根据数据量来看,数据量小的时候,Gson性能要稍微优于FastJson,但在数据量大解析的情况下,FastJson的速度就要明显快于Gson。具体原因,我没研究过,只是做过测试,确实是这样。@H_404_2@
/** * 测试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天天气预报信息) @H_404_2@
/** * @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 + "]";
}
}
/** * 获取网络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的解析:@H_404_2@
/** * 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的解析:@H_404_2@
/** * 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解析:@H_404_2@
/** * 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:@H_404_2@
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);
}
}
}