fastjson的maven的pom_FastJson中的ObjectMapper对象的使用详解

堵景天
2023-12-01

写在前面:开发中经常用到json和对象的相互转换,下面将列出FastJson中ObjectMapper对象的API的使用

一、maven工程中pom导入

com.fasterxml.jackson.core

jackson-databind

2.8.3

二、使用

1、创建对象

public static ObjectMapper mapper = new ObjectMapper();

2、初始化

static {

// 转换为格式化的json

mapper.enable(SerializationFeature.INDENT_OUTPUT);

// 如果json中有新增的字段并且是实体类类中不存在的,不报错

mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

//修改日期格式

mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

}

3、对象转为字符串

String jsonStr = mapper.writeValueAsString(user);

System.out.println("对象转为字符串:" + jsonStr);

4、对象转为byte数组

byte[] byteArr = mapper.writeValueAsBytes(user);

System.out.println("对象转为byte数组:" + byteArr);

5、json字符串转为对象

ObjectClass obj = mapper.readValue(jsonStr, ObjectClass.class);

System.out.println("json字符串转为对象:" + obj);

6、byte数组转为对象

ObjectClass obj = mapper.readValue(byteArr,ObjectClass.class);

System.out.println("byte数组转为对象:" + obj);

7、集合转为字符串

String jsonStr = mapper.writeValueAsString(userList);

System.out.println("集合转为字符串:" + jsonStr);

8、字符串转集合

List list = null;

try {

list = mapper.readValue(jsonStr, List.class);

} catch (IOException e1) {

e1.printStackTrace();

}

9、Map转为字符串

String jsonStr = mapper.writeValueAsString(testMap);

System.out.println("Map转为字符串:" + jsonStr);

10、字符串转Map

Map map = null;

try {

map = mapper.readValue(jsonStr, Map.class);

} catch (IOException e1) {

e1.printStackTrace();

}

三、JsonUtils工具类

import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.DeserializationFeature;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.List;

import java.util.Map;

/**

* @Author Guixing

* @Date 2019/1/7 11:10

* @Description

*/

public class JsonUtils {

public static ObjectMapper mapper = new ObjectMapper();

static {

// 转换为格式化的json

mapper.enable(SerializationFeature.INDENT_OUTPUT);

// 如果json中有新增的字段并且是实体类类中不存在的,不报错

mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

//修改日期格式

mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

}

/**

* 对象转为字符串

*

* @param obj

* @return

*/

public static String Object2Json(Object obj) {

String jsonStr = null;

try {

jsonStr = mapper.writeValueAsString(obj);

} catch (JsonProcessingException e1) {

e1.printStackTrace();

}

return jsonStr;

}

/**

* 对象转为byte数组

*

* @param obj

* @return

*/

public static byte[] object2ByteArray(Object obj) {

byte[] byteArr = new byte[0];

try {

byteArr = mapper.writeValueAsBytes(obj);

} catch (JsonProcessingException e1) {

e1.printStackTrace();

}

return byteArr;

}

/**

* json字符串转为对象

*

* @param jsonStr

* @param beanType

* @param

* @return

*/

public static T json2Object(String jsonStr, Class beanType) {

T t = null;

try {

t = mapper.readValue(jsonStr, beanType);

} catch (IOException e1) {

e1.printStackTrace();

}

return t;

}

/**

* byte数组转为对象

*

* @param byteArr

* @param beanType

* @param

* @return

*/

public static T byteArr2Object(byte[] byteArr, Class beanType) {

T t = null;

try {

t = mapper.readValue(byteArr, beanType);

} catch (Exception e) {

e.printStackTrace();

}

return t;

}

/**

* 集合转为字符串

*

* @param list

* @return

*/

public static String list2String(List list) {

String jsonStr = null;

try {

jsonStr = mapper.writeValueAsString(list);

} catch (JsonProcessingException e1) {

e1.printStackTrace();

}

return jsonStr;

}

/**

* 字符串转集合

*

* @param jsonStr

* @return

*/

public static List json2List(String jsonStr) {

List list = null;

try {

list = mapper.readValue(jsonStr, List.class);

} catch (IOException e1) {

e1.printStackTrace();

}

return list;

}

/**

* Map转为字符串

*

* @param map

* @return

*/

public static String map2String(Map map) {

String jsonStr = null;

try {

jsonStr = mapper.writeValueAsString(map);

} catch (JsonProcessingException e1) {

e1.printStackTrace();

}

return jsonStr;

}

/**

* 字符串转Map

*

* @param jsonStr

* @return

*/

public static Map json2Map(String jsonStr) {

Map map = null;

try {

map = mapper.readValue(jsonStr, Map.class);

} catch (IOException e1) {

e1.printStackTrace();

}

return map;

}

}

————————————————

版权声明:本文为CSDN博主「兴跃神话」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq_37394874/article/details/85992380

 类似资料: