Gson 与 fastJson 在使用上的差异(fastJson的优点)
2017-05-07 21:09
483 查看
一、android 常用的json解析方式
Android 开发上常用的json解析方式有:Gson, fastJson,jackson。因为jackjson jar包会比较大点(700+k),Gson和fastjson的功能又基本够用,所以一直没正式用过jackjson。自己有用的就是Gson和fastjson
二、基本使用方式
Gson
//解成对象 Fromat mFromat = new Gson().fromJson(jsonStringObject, Fromat.class); //解成对象组 LinkedList<Fromat> list = new LinkedList<MainActivity.Fromat>(); Type type = new TypeToken<LinkedList<Fromat>>(){}.getType(); list = new Gson().fromJson(jsonStringArray, type); //泛型统一封装时 需要传个 type 进来 new TypeToken<LinkedList<Fromat>>(){}.getType(); fromJson(String json, Type typeOfT) public <T> T fromJson(String json, Type typeOfT)
fastJson
//解析成对象 Fromat fastjsonObject = JSON.parseObject(jsonObjectString, Fromat.class); //解析成对象组 List<Fromat> fastjsonArray = JSON.parseArray(jsonArrayString, Fromat.class); //泛型统一封装时 需要传个 type 进来 或者TypeReference 也可以也是调用的type new TypeReference<Fromat>() {} new TypeReference<Fromat>() {}.getType() public static <T> T parseObject(String input, Type clazz, Feature... features) public static <T> T parseObject(String text, TypeReference<T> type, Feature... features)
三、fastjson 的优点
可维护性 > 正确性 > 性能说点实在的,Gson在遇到 {} [] 这种json关键字的时候直接解析为string 报错。fastjson是可以解析
demo代码:
String jsonObjectString = "{\"status\": 0}"; String jsonArrayString = "[{\"status\": 0}]"; //fastJson String fastjsonObject = JSON.parseObject(jsonObjectString, String.class); Log.d("test", "fastJson string {} = " + fastjsonObject); String fastjsonArray = JSON.parseObject(jsonArrayString, String.class); Log.d("test", "fastJson string [] = " + fastjsonArray); //Gson try { String gsonObject = new Gson().fromJson(jsonObjectString, String.class); Log.d("test", "gson string {} = " + gsonObject); }catch (Exception e){ Log.d("test", "gson string {} = " + e); } try { String gsonArray = new Gson().fromJson(jsonArrayString, String.class); Log.d("test", "gson string {} = " + gsonArray); }catch (Exception e){ Log.d("test", "gson string [] = " + e); }
结果: D/test: fastJson string {} = {“status”:0} D/test: fastJson string [] = [{“status”:0}] D/test: gson string {} = com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: D/test: gson string [] = com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 1 column 2 path $
from: http://www.voidcn.com/blog/lckj686/article/p-6000736.html
相关文章推荐
- Gson 与 fastJson 在使用上的差异(fastJson的优点)
- JSON技术-Gson将bean转换json确保数据的正确,使用FastJson将Json转换Bean
- Json FastJson Gson介绍与使用
- 使用Gson和FastJson解析JSON数据遇到的问题
- 使用Gson和fastjson格式化JSON字符串
- Andorid中使用Gson和Fast-json解析库解析JSON数据---第三方库学习笔记(二)
- FastJson Jackson Gson使用教程
- JSON-JAVA(org.json),GSON,FASTJSON的使用
- Android使用GSON和FASTJSON解析服务器返回的JSON数据
- Android常用Json解析框架GSON和FastJson的使用
- json解析库gson和fastjson使用
- Gson 与 fastJson 的使用
- Object转Json差异之Gson fastJson jackson 修改字段名
- Android中Json数据解析(二)--使用Gson、Jackson和FastJson解析Json数据
- Gson和Fastjson的使用
- Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例
- Android 中Json解析的几种框架(Gson、Jackson、FastJson、LoganSquare)使用与对比
- android JSON解析 fastjson和gson的使用
- Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例
- Gson 与 Fastjson 使用的初步详解