Java中使用FastJSON進行對象的序列化和反序列化java
1.添加依賴,maven的pom.xml文件中添加如下依賴json
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency>
2.FastJSON序列化和反序列化工具類maven
import java.util.List; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; // JSON/對象轉換類 public class JsonUtils { /** * JSON字符串轉換成對象 */ public static <T> T jsonStringToObject(String jsonStr,Class<T> obj){ try{ return JSONObject.parseObject(jsonStr, obj); }catch(Exception e){ System.out.println("將JSON串{}轉換成 指定對象失敗:"+jsonStr); e.printStackTrace(); } return null; } /** * 對象轉JSON */ public static <T> String objectToJson(T obj){ try{ String json=JSONObject.toJSONString(obj); return json; }catch(Exception e){ System.out.println("將指定對象轉成JSON串{}失敗:"+obj.toString()); e.printStackTrace(); } return ""; } /** * List<T>對象轉成json */ public static<T> String listToJsonString(List<T> objList){ try{ String json=JSONObject.toJSONString(objList); return json; }catch(Exception e){ System.out.println("將對象列表轉成JSON串{}失敗:"+objList.toString()); e.printStackTrace(); } return ""; } /** * json轉換成對象列表List<T> */ public static <T> List<T> jsonStringToList(String json,Class<T> obj){ try{ return JSONArray.parseArray(json, obj); }catch(Exception e){ System.out.println("將JSON串{}轉成對象列表失敗:"+json); e.printStackTrace(); } return null; } /* * 將JSON串轉換爲JSONOBJECT */ public static JSONObject stringTOJSONObject(String json){ JSONObject jsonObject = null; try { jsonObject = JSONObject.parseObject(json); } catch (Exception e) { System.out.println("JSON串{} 轉換成 JsonObject失敗"+json); } return jsonObject; } }
3.使用ide
(1)序列化工具
List<User> users=new ArrayList<User>(); for(int i=0;i<20;i++){ User user=new User(); user.setName("FastJSON"+i); user.setAge(20+i); SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd"); user.setBirthday(dateformat.parse("1991-10-01")); user.setEmail("12345678"+i+"@qq.com"); // 序列化單個對象 String json=JsonUtils.objectToJson(user); users.add(user); // 序列化對象列表 // String json=JsonUtils.listToJsonString(users); }
(2)反序列化code
// 反序列化單個對象 User u=JsonUtils.jsonStringToObject(json, User.class); System.out.println(u.toString()); // 將JSON串轉換爲JSONOBJECT JSONObject js=JsonUtils.stringTOJSONObject(json); System.out.println(js); // 反序列化對象列表 List<User> list=JsonUtils.jsonStringToList(json, User.class); System.out.println(list);
4.FastJson的常見操做orm
(1)日期進行格式化 String dateToJson=JSON.toJSONString(new Date(), SerializerFeature.WriteDateUseDateFormat); System.out.println("使用JSON中的SerializerFeature特性格式化日期:"+dateToJson); String dateToJson2=JSON.toJSONStringWithDateFormat(new Date(), "yyyy/MM/dd"); System.out.println("JSON中自定義日期的輸出格式:"+dateToJson2); (2)將Map轉換成JSONObject而後添加元素,最後輸出。 Map<String, Object> map = new HashMap<String, Object>(); map.put("name", "Gelpe"); map.put("age", 30); map.put("brithday",dateToJson); map.put("date",dateToJson2); // 將Map對象轉成JSON字符串 String mapToJson=JSON.toJSONString(map); System.out.println("將MAP轉成JSON: :"+mapToJson); // 將Map轉換成JSONObject對象 JSONObject jsonObject = new JSONObject(map); System.out.println("JSONObject 1:"+jsonObject); (3)List轉成JSON List<String> list=new ArrayList<String>(); list.add("shopping"); list.add("travel"); String listToJson=JSON.toJSONString(list); System.out.println("將List中存儲的字符串轉成JSON: :"+listToJson); (4) 向JSONObject中添加值 jsonObject.put("hobbies",list); (5) 從JSONObject中取值 System.out.println(jsonObject.get("hobbies")); System.out.println("JSONObject 2:"+jsonObject); (6)將List對象轉成JSONArray,而後輸出 List<Map<String,Object>> mapList=new ArrayList<Map<String,Object>>(); Map<String, Object> map3 = new HashMap<String, Object>(); map3.put("address", "Beijing"); map3.put("city", "Beijing"); Map<String, Object> map4 = new HashMap<String, Object>(); map4.put("town", "wulukou"); map4.put("street", "Four street"); mapList.add(map3); mapList.add(map4); String listMapToJson=JSON.toJSONString(mapList); System.out.println("將List中存儲的MAP對象轉成JSON:"+listMapToJson); (7)JSON格式化輸出 // 第二個參數true表示是否格式化輸出 String listMapToJson2=JSON.toJSONString(mapList, true); System.out.println("將List中存儲的MAP對象轉成JSON而且格式化輸出:"+listMapToJson2); (8) 將List對象轉成JSONArray JSONArray jsonArray=JSONArray.parseArray(JSON.toJSONString(mapList)); System.out.println("JSONArray:"+jsonArray); (9)打印JSONArray中的內容 for(int i=0;i<jsonArray.size();i++){ System.out.println("JSONArray Content:"+jsonArray.get(i)); } (10)將JSONArray放到JSONObject中 jsonObject.put("addressInfo",jsonArray); System.out.println("JSONObject 3:"+jsonObject); (11) 自定義對象轉成JSON User u=new User(); u.setName("NB Person"); u.setAge(24); u.setBirthday(new Date()); // 自定義對象轉成JSON String userJson=JSON.toJSONString(u); System.out.println("自定義對象轉成JSON:"+userJson);
以上操做,輸出的內容:xml
使用JSON中的SerializerFeature特性格式化日期:"2018-12-01 10:58:46" JSON中自定義日期的輸出格式:"2018/12/01" 將MAP轉成JSON: :{"date":"\"2018/12/01\"","brithday":"\"2018-12-01 10:58:46\"","name":"Gelpe","age":30} JSONObject 1:{"date":"\"2018/12/01\"","brithday":"\"2018-12-01 10:58:46\"","name":"Gelpe","age":30} 將List中存儲的字符串轉成JSON: :["shopping","travel"] [shopping, travel] JSONObject 2:{"date":"\"2018/12/01\"","hobbies":["shopping","travel"],"brithday":"\"2018-12-01 10:58:46\"","name":"Gelpe","age":30} 將List中存儲的MAP對象轉成JSON:[{"address":"Beijing","city":"Beijing"},{"town":"wulukou","street":"Four street"}] 將List中存儲的MAP對象轉成JSON而且格式化輸出:[ { "address":"Beijing", "city":"Beijing" }, { "town":"wulukou", "street":"Four street" } ] JSONArray:[{"address":"Beijing","city":"Beijing"},{"town":"wulukou","street":"Four street"}] JSONArray Content:{"address":"Beijing","city":"Beijing"} JSONArray Content:{"town":"wulukou","street":"Four street"} JSONObject 3:{"date":"\"2018/12/01\"","addressInfo":[{"address":"Beijing","city":"Beijing"},{"town":"wulukou","street":"Four street"}],"hobbies":["shopping","travel"],"brithday":"\"2018-12-01 10:58:46\"","name":"Gelpe","age":30} 自定義對象轉成JSON:{"age":24,"birthday":1543648393681,"name":"NB Person"}
JSON的解析:對象
(1)集合反序列化 Map<String,String> map=JSON.parseObject(jsonString, Map.class); System.out.println("map get name:"+map.get("name")); System.out.println("map:"+map); (2)泛型的反序列化,使用TypeReference傳入類型信息 Map<String,String> map2=JSON.parseObject((jsonString,new TypeReference<Map<String,String>>(){}); System.out.println("map2 get name:"+map2.get("name")); System.out.println("map2 get hobbies:"+map2.get("hobbies")); System.out.println("map2:"+map2);