这么把⼀个map对象序列化_Fastjson序列化与反序列化JAVA
五种常⽤对象
1.Fastjson简介
Fastjson是阿⾥巴巴的开源JSON解析库,⽀持任何JAVA对象与JSON格式的序列化与反序列化;
序列化:JAVA对象转换为JSON字符串;反序列化:JSON字符串转换为JAVA对象;
提供服务器端、安卓客户端两种解析⼯具,性能表现较好;
允许转换预先存在的⽆法修改的对象(只有class、⽆源代码);
Java泛型的⼴泛⽀持;允许对象的⾃定义表⽰、允许⾃定义序列化类;
⽀持任意复杂对象(具有深厚的继承层次和⼴泛使⽤的泛型类型);
2.下载使⽤
Maven中央仓库下载最新版本:
/artifact/com.alibaba/fastjson/1.2.75/jar
或者配置maven坐标依赖:
com.alibaba  fastjson  1.2.75
3.⾃定义类User.java
ity;public class User {  private Integer id;  private String name;  private String pass;  private Integer age;  public User() {  }  public Use
4.序列化为JSON字符串
json转换对象以下针对Java五种常⽤对象:JavaBean,Array,List,Set和Map序列化为JSON字符串。
4.1 ⾃定义JavaBean对象序列化为JSON字符串:
/*** pojo/domain/entity ==> json*
Title: bean2Json
*
Description:
*/public static void bean2Json(){ User user = new User(1001,"jack","jack",18); String jsonString = JSONString(user);
logger.info(jsonFormatter(jsonString,"bean"));}
4.2 Array/List/Set/Map容器对象序列化为JSON字符串:
/*** array/list/set/map ==> json*
Title: collection2Json
*
Description:
*/public static void collection2Json() { User[] array = new User[2]; //Array List list = new ArrayList(); //List Set set = new HashSet(); //Set Map map = new HashMap();//Map for(int i = 0; i < 2; i++) { array[i] = new User(i, "raky-array-" + i,"" + i, i); list.add(new User(i, "raky-list-" + i,"" + i, i)); set.add(new User(i, "raky-set-" + i,"" + i, i)); map.put(i, new User(i, "raky-map-"+i,"" + i, i)); } String arrayJsonString =
logger.info(jsonFormatter(listJsonString,"list")); logger.info(jsonFormatter(setJsonString,"set"));
logger.info(jsonFormatter(mapJsonString,"bean"));}
5.反序列化为Java对象
以下针对JSON字符串转换为Java五种常⽤对象:JavaBean,Array,List,Set和Map。
5.1 JSON字符串反序列化为⾃定义JavaBean对象:
/*** json ==> pojo/domain/entity*
Title: json2Bean
*
Description:
*/public static void json2Bean(){ String jsonString = "{"id":1001,"name":"jack","pass":"jack","age":18}"; User user =
JSON.parseObject(jsonString, User.class); logger.String());}
5.2 JSON字符串反序列化为Array/List/Set/Map容器对象:
/*** json ==> array/list/set/map*
Title: json2Collection
*
Description:
*/public static void json2Collection() { String arrayJsonString = "[{"id":0,"name":"raky-array-0","pass":"0","age":0},{"id":1,"name":"raky-array-1","pass":"1","age":1}]"; String listJsonString = "[{"id":0,"name":"raky-list-0","pass":"0","age":0},{"id":1,"name":"raky-list-1","pass":"1","age":1}]"; String setJsonString = "[{"id":1,"name":"raky-set-1","pass":"1","age":1},{"id":0,"name":"raky-set-0","pass":"0","age":0}]"; String mapJsonString = "{"0":{"id":0,"name":"raky-map-0","pass":"0","age":0},"1":{"id":1,"name":"raky-map-1","pass":"1","age":1}}"; User[] array = JSON.parseObject(arrayJsonString, User[].class); //List list = JSON.parseArray(listJsonString, User.class); List list = JSON.parseObject(listJsonString, new TypeReference>() {});Set set = JSON.parseObject(setJsonString, new TypeReference>() {});Map map = JSON.parseObject(mapJsonString, new TypeReference>() {});Arrays.asList(array).forEach((value) -> System.out.println("array-value => " + value));list.forEach((value) -> System.out.println("list-value => " + value));set.forEach((value) -> System.out.println("set-value => " + value));map.forEach((key,value) -> System.out.println("map-value => key = " + key + ", value = " + value));}
6.Fastjson格式化输出
/*** 格式化json*
Title: jsonFormatter
*
Description:
* @param jsonString* @param type* @return*/public static String jsonFormatter(String jsonString, String type){ Object object = null;
if("bean.map".contains(type)) { //bean/map JSONObject jsonObject = JSONObject.parseObject(jsonString); object = jsonObject; }else
if("array.list.set".contains(type)) { //array/list/set JSONArray jsonArray = JSONArray.parseArray(jsonString); object = jsonArray; } String prettyJsonString = JSONString(object, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat); return prettyJsonString;}
⾄此搞定,Fastjson序列化与反序列化五种常⽤对象就这么简单,⼩伙伴们不妨试试。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。