目录
自己基于jackson封装的json转换数据工具类
提供实体转json,json转实体,以及jsonNode的获取方
工具类
package com.example.fl.fuli.util;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import java.io.IOException;
import java.text.SimpleDateFormat;
public class JsonUtil {
private static ObjectMapper mapper = new ObjectMapper();
static {
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
public static void setDateFormat(String pattern) {
mapper.setDateFormat(new SimpleDateFormat(pattern));
}
public static void setNamingStrategy(PropertyNamingStrategy propertyNamingStrategy) {
mapper.setPropertyNamingStrategy(propertyNamingStrategy);
}
public static void unwrapRootValue() {
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
}
public static void wrapRootValue() {
mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
}
public static void setFilterProvider(FilterProvider filterProvider) {
mapper.setFilterProvider(filterProvider);
}
public static String objectToJson(Object obj) {
try {
return mapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
throw new RuntimeException("Failed to convert object to json", e);
}
}
public static <T> T jsonToObject(String responseJson, Class<T> clazz) {
try {
return mapper.readValue(responseJson, clazz);
} catch (IOException e) {
throw new RuntimeException("Failed to convert json to object", e);
}
}
public static <T> T jsonToObject(String responseJson, TypeReference<T> valueTypeRef) {
try {
return mapper.readValue(responseJson, valueTypeRef);
} catch (IOException e) {
throw new RuntimeException("Failed to convert json to object", e);
}
}
public static JsonNode getJsonNode(String jsonStr) {
try {
return mapper.readTree(jsonStr);
} catch (JsonProcessingException e) {
throw new RuntimeException("Failed to convert request to JsonNode",
e);
} catch (IOException e) {
throw new RuntimeException("IO exception", e);
}
}
}
例子:
对象:Person
例1:对象转json 和 json转对象
Person person=new Person();
person.setName("张三");
person.setAge(10);
person.setDescription("张三,十岁了");
//转换为json
String json = JsonUtil.objectToJson(person);
// {"name":"张三","age":10,"description":"张三,十岁了"}
System.out.println(json);
//json转换为对象
Person person1 = JsonUtil.jsonToObject(json, Person.class);
// Person(name=张三, age=10, description=张三,十岁了),这里应该是在IDEA中转换的问题,实际应该为{"name":"张三","age":10,"description":"张三,十岁了"}
System.out.println(person1);
例2:集合转json和 json转集合
Person person=new Person();
person.setName("张三");
person.setAge(10);
person.setDescription("张三,十岁了");
Person person2=new Person();
person2.setName("李四");
person2.setAge(11);
person2.setDescription("李四,十一岁了");
Person person3=new Person();
person3.setName("王五");
person3.setAge(12);
person3.setDescription("王五,十二岁了");
List<Person> list = new ArrayList<Person>();
list.add(person);
list.add(person2);
list.add(person3);
//集合转json
String json = JsonUtil.objectToJson(list);
//[{"name":"张三","age":10,"description":"张三,十岁了"},{"name":"李四","age":11,"description":"李四,十一岁了"},{"name":"王五","age":12,"description":"王五,十二岁了"}]
System.out.println(json);
//json转集合(因为知道类型,所以可以直接转换)
List <Person>object = JsonUtil.jsonToObject(json, List.class);
//[{"name":"张三","age":10,"description":"张三,十岁了"},{"name":"李四","age":11,"description":"李四,十一岁了"},{"name":"王五","age":12,"description":"王五,十二岁了"}]
System.out.println(object);