fastjson和gson对json中数据类型和格式的兼容性对比

前端之家收集整理的这篇文章主要介绍了fastjson和gson对json中数据类型和格式的兼容性对比前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

网上对比解析框架性能优劣的帖子一大堆,但是很少有人针对这几个框架对数据格式和类型的兼容性分析,实际开发中由于客户端和服务端协调不到位引起的这些问题还是值得注意的。

结论如下:
1.格式不对照:json支持兼容更好,部分内容格式不对照仍可以解析成功。
2.key不对照:同样支持key不对照或缺少情况
3.value类型不对照(重点):int-float都支持,float-int只有json支持,”“-float/int只有json支持,String解析为数值类型都是不支持的 Json兼容性支持更好。
4.泛型嵌套:同样支持泛型嵌套
5.内部类嵌套:同样支持内部类嵌套,但是fastjson对嵌套的内部类要求第三层内部类是静态的才可以正确解析,否则会异常如下:
com.alibaba.fastjson.JSONException: create instance error,class com.example.pcbin.myapplication.jsonvsgson.NestingBean$DataThree
,而gson没有这个要求。

总结:总的来说fastjson兼容性支持更好。改成fastjson注意排查所有三层嵌套的bean,第三层bean必须为static。

测试代码如下:

private void testJson() {
    Log.e("--","---------------------------格式不对照------------------------------");
    String jsonStr = "{\"status\":1,\"tips\":\"操作成功\",\"data\":{}}";
    String jsonError = "{\"status\":1,\"data\":[]}";

    BaseBean resultBeanGson = new Gson().fromJson(jsonStr,BaseBean.class);
    Log.e("格式不对照-Gson-正确格式:",resultBeanGson.toString());
    try {
      BaseBean resultBeanGsonStyle = new Gson().fromJson(jsonError,BaseBean.class);
      Log.e("格式不对照-Gson-错误格式:",resultBeanGsonStyle.toString());
    }catch (Exception e){
      Log.e("格式不对照-Gson-错误格式:",e.toString());
    }
    BaseBean resultBeanJson = JSON.parSEObject(jsonStr,BaseBean.class);
    BaseBean resultBeanJsonStyle = JSON.parSEObject(jsonError,BaseBean.class);
    Log.e("格式不对照-JSON-正确格式:",resultBeanJson.toString());
    Log.e("格式不对照-JSON-错误格式:",resultBeanJsonStyle.toString());

    Log.e("--","---------------------------格式不对照22222--------------------------");
    String jsonStr22 = "{\n" +
            "    \"status\": 100000002,\n" +
            "    \"tips\": \"データがありません\",\n" +
            "    \"data\": {}\n" +
            "}";
    String jsonError22 = "{\n" +
            "    \"status\": 1,\n" +
            "    \"tips\": \"操作成功\",\n" +
            "    \"data\": [\n" +
            "        {\n" +
            "            \"iurl\": \"https://zerovideotest.oss-cn-hangzhou.aliyuncs.com/dobby/ad2.png\",\n" +
            "            \"url\": \"http://bbs.zerotech.com/thread-1407-1-1.html\",\n" +
            "            \"md5\": \"85ea22e4c26dfc0ef73a622e81d61ca8\",\n" +
            "            \"lang\": \"zh-Hans\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"iurl\": \"https://zerovideotest.oss-cn-hangzhou.aliyuncs.com/dobby/ad3.png\",\n" +
            "            \"url\": \"http://bbs.zerotech.com/thread-1539-1-1.html\",\n" +
            "            \"md5\": \"58241a06cfc1c955e21e9c242d22ed27\",\n" +
            "            \"lang\": \"zh-Hans\"\n" +
            "        }\n" +
            "    ]\n" +
            "}";
    try {
      BaseBean2 resultBeanGson22 = new Gson().fromJson(jsonStr22,BaseBean2.class);
      Log.e("22格式不对照-Gson-正确格式:",resultBeanGson22.toString());
      BaseBean2 resultBeanGsonStyle22 = new Gson().fromJson(jsonError22,BaseBean2.class);
      Log.e("22格式不对照-Gson-错误格式:",resultBeanGsonStyle22.toString());
    }catch (Exception e){
      Log.e("22格式不对照-Gson-错误格式:",e.toString());
    }
    BaseBean2 resultBeanJson22 = JSON.parSEObject(jsonStr22,BaseBean2.class);
    BaseBean2 resultBeanJsonStyle22 = JSON.parSEObject(jsonError22,BaseBean2.class);
    Log.e("22格式不对照-JSON-正确格式:",resultBeanJson22.toString());
    Log.e("22格式不对照-JSON-错误格式:",resultBeanJsonStyle22.toString());

    Log.e("--","---------------------------key不对照------------------------------");
    String jsonStr2 = "{\"status\":1,\"data\":{\"name\":\"chao\",\"age\":18}}";
    String jsonError2 = "{\"status\":1,\"ageError\":18}}";

    UserBean resultBeanGson2 = new Gson().fromJson(jsonStr2,UserBean.class);
    UserBean resultBeanGsonStyle2 = new Gson().fromJson(jsonError2,UserBean.class);
    Log.e("key不对照-Gson-正确格式:",resultBeanGson2.toString());
    Log.e("key不对照-Gson-错误格式:",resultBeanGsonStyle2.toString());
    UserBean resultBeanJson2 = JSON.parSEObject(jsonStr2,UserBean.class);
    UserBean resultBeanJsonStyle2 = JSON.parSEObject(jsonError2,UserBean.class);
    Log.e("key不对照-JSON-正确格式:",resultBeanJson2.toString());
    Log.e("key不对照-JSON-错误格式:",resultBeanJsonStyle2.toString());

    Log.e("--","---------------------------value类型不对照------------------------------");
    String jsonStr3 = "{\"status\":1,\"data\":{\"name\":18,\"age\":18.1}}";
    String jsonError3 = "{\"status\":1,\"age\":\"\"}}";

    UserBeanType resultBeanGson3 = new Gson().fromJson(jsonStr3,UserBeanType.class);
    Log.e("value类型不对照-Gson-正确格式:",resultBeanGson3.toString());
    try {
      UserBeanType resultBeanGsonStyle3 = new Gson().fromJson(jsonError3,UserBeanType.class);
      Log.e("value类型不对照-Gson-错误格式:",resultBeanGsonStyle3.toString());
    }catch (Exception e){
      Log.e("value类型不对照-Gson-错误格式:",e.toString());
    }
    UserBeanType resultBeanJson3 = JSON.parSEObject(jsonStr3,UserBeanType.class);
    Log.e("value类型不对照-JSON-正确格式:",resultBeanJson3.toString());
    try{
      UserBeanType resultBeanJsonStyle3 = JSON.parSEObject(jsonError3,UserBeanType.class);
      Log.e("value类型不对照-JSON-错误格式:",resultBeanJsonStyle3.toString());
    }catch (Exception e){
      Log.e("value类型不对照-JSON-错误格式:",e.toString());
    }


    Log.e("--","---------------------------泛型嵌套------------------------------");
    String jsonStr4 = "{\"status\":1,\"age\":18}}";

    BaseType<DataBean> resultBeanGson4 = new Gson().fromJson(jsonStr4,new BaseType<DataBean>().getClass());
    Log.e("泛型嵌套-Gson-正确格式:",resultBeanGson4.toString());
    BaseType<DataBean> resultBeanJson4 = JSON.parSEObject(jsonStr4,new BaseType<DataBean>().getClass());
    Log.e("泛型嵌套-JSON-正确格式:",resultBeanJson4.toString());

    Log.e("--","---------------------------内部类嵌套------------------------------");
//    String jsonStr5 = "{\"status\":1,\"age\":18}}";
    String jsonStr5 = "{\"status\":1,\"age\":18,\"three\":{\"namethree\":\"yc\"}}}";

    NestingBean resultBeanGson5 = new Gson().fromJson(jsonStr5,new NestingBean().getClass());
    Log.e("内部类嵌套-Gson-正确格式:",resultBeanGson5.toString());
    try {
      NestingBean resultBeanJson5 = JSON.parSEObject(jsonStr5,new NestingBean().getClass());
      Log.e("内部类嵌套-JSON-正确格式:",resultBeanJson5.toString());
    }catch (Exception e){
      Log.e("内部类嵌套-JSON-错误格式",e.toString());
    }
  }

猜你在找的Json相关文章