一、创建对象并初始化
public class JsonTest {
private static ObjectMapper objectMapper = new ObjectMapper();
static {
//config
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
// 如果json中有新增的字段并且是实体类类中不存在的,不报错
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//修改日期格式
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
}
}
二、 objectMapper.writeValueAsString() 与 objectMapper.readValue()的用法
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("张三");
list.add("张五");
Map<String,Object> map = new HashMap<>();
map.put("姓名","王五");
map.put("姓别","男");
UserTest userTest = new UserTest();
userTest.setName("法外狂徒张三");
userTest.setAge("20");
userTest.setTime(new Date());
try {
//通过writeValueAsString方法 可以把 对象转json串、List转Json串、Map转json串(这个过程被称为序列化)
String list1 = objectMapper.writeValueAsString(list);
//通过readValue方法 可以把 json串转为:对象、List、Map(这个过程被称为反序列化)
List list2 = objectMapper.readValue(list1, List.class);
System.out.println("list1:"+list1);
System.out.println("list2:"+list2);
String map1 = objectMapper.writeValueAsString(map);
Map map2 = objectMapper.readValue(map1, Map.class);
System.out.println("map1:"+map1);
System.out.println("map2:"+map2);
String userTest1 = objectMapper.writeValueAsString(userTest);
UserTest userTest2 = objectMapper.readValue(userTest1,UserTest.class);
System.out.println("userTest1:"+userTest1);
System.out.println("userTest2:"+userTest2
);
} catch (Exception e) {
e.printStackTrace();
}
}
}
控制台输出:
通过控台可以看出:
通过writeValueAsString方法 可以把 对象转json串、List转Json串、Map转json串
通过readValue方法 可以把 json串转为:对象、List、Map
(userTest2 json转实体类 时间格式没有修改)
三、ObjectMapper提供的readTree,返回JsonNode对象
ObjectMapper mapper = new ObjectMapper();
String message="{\"code\":\"EXCEPTION\",\"msgInfo\":\"交易支付异常\"} ";
//获得json对象
JsonNode jsonNode = mapper.readTree(message);
//获取其中code属性值
JsonNode code=jsonNode.get("code");
//获取文本值
//这个方法会直接返回value的值,不会对其进行任何的包裹
String codeTwo=code.asText()
四、ObjectMapper工具类
package cn.edu.buaa.bdbc.cyscc.yxj.apply.util;
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 MR.GAI
* @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 <T>
* @return
*/
public static <T> T json2Object(String jsonStr, Class<T> beanType) {
T t = null;
try {
t = mapper.readValue(jsonStr, beanType);
} catch (IOException e1) {
e1.printStackTrace();
}
return t;
}
/**
* byte数组转为对象
*
* @param byteArr
* @param beanType
* @param <T>
* @return
*/
public static <T> T byteArr2Object(byte[] byteArr, Class<T> 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;
}
}
如有问题 ,请大家多多指教。