很多朋友都想知道java fastjson是什么?下面就一起來了解一下吧~
1、FastJson的介紹:
JSON(javaScript Object Notation)是一種輕量級的數據交換格式。主要采用鍵值對({"name": "json"})的方式來保存和表示數據。JSON是JS對象的字符串表示法,它使用文本表示一個JS對象的信息,本質上是一個字符串。
JSON的處理器有很多,這里我介紹一下FastJson,FastJson是阿里的開源JSON解析庫,可以解析JSON格式的字符串,支持將Java Bean序列化為JSON字符串,也可以從JSON字符串反序列化到JavaBean。是一個極其優秀的Json框架,Github地址:?FastJson
2、FastJson的特點:
1.FastJson數度快,無論序列化和反序列化,都是當之無愧的fast?2.功能強大(支持普通JDK類包括任意Java Bean Class、Collection、Map、Date或enum)?3.零依賴(沒有依賴其它任何類庫)
3、FastJson的用法:
首先定義三個json格式的字符串
//json字符串-簡單對象型 private?static?final?String??JSON_OBJ_STR?=?"{\"studentName\":\"lily\",\"studentAge\":12}"; //json字符串-數組類型 private?static?final?String??JSON_ARRAY_STR?=?"[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]"; //復雜格式json字符串 private?static?final?String??COMPLEX_JSON_STR?=?"{\"teacherName\":\"crystall\",\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}";
4、簡單javaBean與json對象之間的轉換
*?簡單JavaBean_obj到json對象的轉換 ?*/ @Test public?void?testJavaBeanToJSONObject(){ ????//已知簡單JavaBean_obj ????Student?student?=?new?Student("lily",?12); ????//方式一 ????String?jsonString?=?JSONObject.toJSONString(student); ????JSONObject?jsonObject?=?JSONObject.parseObject(jsonString); ????System.out.println(jsonObject); ????//方式二 ????JSONObject?jsonObject1?=?(JSONObject)?JSONObject.toJSON(student); ????System.out.println(jsonObject1); } /** ?*?簡單json對象到JavaBean_obj的轉換 ?*/ @Test public?void?testJSONObjectToJavaBean(){ ????//已知簡單json對象 ????JSONObject?jsonObject?=?JSONObject.parseObject(JSON_OBJ_STR); ????//第一種方式,使用TypeReference類,由于其構造方法使用protected進行修飾,故創建其子類 ????Student?student?=?JSONObject.parseObject(jsonObject.toJSONString(),?new?TypeReference ()?{}); ????System.out.println(student); ????//第二種方式,使用Gson的思想 ????Student?student1?=?JSONObject.parseObject(jsonObject.toJSONString(),?Student.class); ????System.out.println(student1); }
5、JavaList與JsonArray之間的轉換
/** ?*?JavaList到JsonArray的轉換 ?*/ @Test public?void?testJavaListToJsonArray()?{ ????//已知JavaList ????Student?student?=?new?Student("lily",?12); ????Student?studenttwo?=?new?Student("lucy",?15); ????List?students?=?new?ArrayList (); ????students.add(student); ????students.add(studenttwo); ????//方式一 ????String?jsonString?=?JSONArray.toJSONString(students); ????JSONArray?jsonArray?=?JSONArray.parseArray(jsonString); ????System.out.println(jsonArray); ????//方式二 ????JSONArray?jsonArray1?=?(JSONArray)?JSONArray.toJSON(students); ????System.out.println(jsonArray1); } /** ?*?JsonArray到JavaList的轉換 ?*/ @Test public?void?testJsonArrayToJavaList()?{ ????//已知JsonArray ????JSONArray?jsonArray?=?JSONArray.parseArray(JSON_ARRAY_STR); ????//第一種方式,使用TypeReference 類,由于其構造方法使用protected進行修飾,故創建其子類 ????ArrayList ?students?=?JSONArray.parseObject(jsonArray.toJSONString(), ????????????new?TypeReference >()?{}); ????System.out.println(students); ????//第二種方式,使用Gson的思想 ????List ?students1?=?JSONArray.parseArray(jsonArray.toJSONString(),?Student.class); ????System.out.println(students1); }
6、JavaList與JsonArray之間的轉換
/** ?*?JavaList到JsonArray的轉換 ?*/ @Test public?void?testJavaListToJsonArray()?{ ????//已知JavaList ????Student?student?=?new?Student("lily",?12); ????Student?studenttwo?=?new?Student("lucy",?15); ????List?students?=?new?ArrayList (); ????students.add(student); ????students.add(studenttwo); ????//方式一 ????String?jsonString?=?JSONArray.toJSONString(students); ????JSONArray?jsonArray?=?JSONArray.parseArray(jsonString); ????System.out.println(jsonArray); ????//方式二 ????JSONArray?jsonArray1?=?(JSONArray)?JSONArray.toJSON(students); ????System.out.println(jsonArray1); } /** ?*?JsonArray到JavaList的轉換 ?*/ @Test public?void?testJsonArrayToJavaList()?{ ????//已知JsonArray ????JSONArray?jsonArray?=?JSONArray.parseArray(JSON_ARRAY_STR); ????//第一種方式,使用TypeReference 類,由于其構造方法使用protected進行修飾,故創建其子類 ????ArrayList ?students?=?JSONArray.parseObject(jsonArray.toJSONString(), ????????????new?TypeReference >()?{}); ????System.out.println(students); ????//第二種方式,使用Gson的思想 ????List ?students1?=?JSONArray.parseArray(jsonArray.toJSONString(),?Student.class); ????System.out.println(students1); }
7、復雜JavaBean_obj與json對象之間的轉換/**
*?復雜JavaBean_obj到json對象的轉換 ?*/ @Test public?void?testComplexJavaBeanToJSONObject()?{ ????//已知復雜JavaBean_obj ????Student?student?=?new?Student("lily",?12); ????Student?studenttwo?=?new?Student("lucy",?15); ????List?students?=?new?ArrayList (); ????students.add(student); ????students.add(studenttwo); ????Course?course?=?new?Course("english",?1270); ????Teacher?teacher?=?new?Teacher("crystall",?27,?course,?students); ????//方式一 ????String?jsonString?=?JSONObject.toJSONString(teacher); ????JSONObject?jsonObject?=?JSONObject.parseObject(jsonString); ????System.out.println(jsonObject); ????//方式二 ????JSONObject?jsonObject1?=?(JSONObject)?JSONObject.toJSON(teacher); ????System.out.println(jsonObject1); } /** ?*?復雜json對象到JavaBean_obj的轉換 ?*/ @Test public?void?testComplexJSONObjectToJavaBean()?{ ????//已知復雜json對象 ????JSONObject?jsonObject?=?JSONObject.parseObject(COMPLEX_JSON_STR); ????//第一種方式,使用TypeReference 類,由于其構造方法使用protected進行修飾,故創建其子類 ????Teacher?teacher?=?JSONObject.parseObject(jsonObject.toJSONString(),?new?TypeReference ()?{}); ????System.out.println(teacher); ????//第二種方式,使用Gson的思想 ????Teacher?teacher1?=?JSONObject.parseObject(jsonObject.toJSONString(),?Teacher.class); ????System.out.println(teacher1); }
總結
//?把JSON文本parse為JSONObject或者JSONArray ????????public?static?final?Object?parse(String?text);?? ????????//?把JSON文本parse成JSONObject ????????public?static?final?JSONObject?parseObject(String?text); ????????//?把JSON文本parse為JavaBean ????????public?static?final??T?parseObject(String?text,?Class ?clazz);? ????????//?把JSON文本parse成JSONArray ????????public?static?final?JSONArray?parseArray(String?text);? ????????//把JSON文本parse成JavaBean集合 ????????public?static?final? ?List ?parseArray(String?text,?Class ?clazz);????? ????????//?將JavaBean序列化為JSON文本 ????????public?static?final?String?toJSONString(Object?object);??????? ????????//?將JavaBean序列化為帶格式的JSON文本 ????????public?static?final?String?toJSONString(Object?object,?boolean?prettyFormat);? ????????//將JavaBean轉換為JSONObject或者JSONArray。 ????????public?static?final?Object?toJSON(Object?javaObject);
以上就是小編今天的分享,希望可以幫到大家。