Jackson——对象转json/对象转json
public class JackSon_Test_01 {
@Data
@ToString
public static class Person {
private int id;
private String name;
private String password;
public Person() {
super();
}
public Person(int id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
}
public static void main(String[] args) throws Exception{
Person startObject = new Person(1, "tom", "123");
ObjectMapper objectMapper=new ObjectMapper();
String startObjectJson = objectMapper.writeValueAsString(startObject);
System.out.println("startObjectJson: " + startObjectJson);
Person deSerializationObject = objectMapper.readValue(startObjectJson, Person.class);
System.out.println(deSerializationObject.toString());
}
}
Jackson——json转数组/数组转json
public static void main(String[] args) throws Exception{
ObjectMapper objectMapper = new ObjectMapper();
List<Person> personList = new ArrayList<>();
personList.add(new Person(1, "tom", "123"));
personList.add(new Person(2, "jack", "123445"));
String jsonArrayString = objectMapper.writeValueAsString(personList);
System.out.println("jsonArrayString: " + jsonArrayString);
JavaType javaType = objectMapper
.getTypeFactory()
.constructParametricType(List.class, Person.class);
List<Person> list = objectMapper.readValue(jsonArrayString,javaType);
System.out.println(list.toString());
}
Jackson——objectMapper基本配置
ObjectMapper mapper=new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
mapper.setSerializationInclusion(JsonInclude.Include.NON_ABSENT);
mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
mapper.setSerializationInclusion(JsonInclude.Include.USE_DEFAULTS);
mapper.configure(SerializationFeature.EAGER_SERIALIZER_FETCH,false);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES,false);
mapper.configure(MapperFeature.AUTO_DETECT_CREATORS,false);
Jackson——工具类
public class JackSonUtils {
private static final ObjectMapper MAPPER = new ObjectMapper();
static {
MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
MAPPER.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
}
public static String objectToJson(Object data) {
try {
String result = MAPPER.writeValueAsString(data);
return result;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
public static <T> T jsonToBean(String jsonData, Class<T> beanType) {
try {
T result = MAPPER.readValue(jsonData, beanType);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) {
JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
try {
List<T> resultList = MAPPER.readValue(jsonData, javaType);
return resultList;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static <K, V> Map<K, V> jsonToMap(String jsonData, Class<K> keyType, Class<V> valueType) {
JavaType javaType = MAPPER.getTypeFactory().constructMapType(Map.class, keyType, valueType);
try {
Map<K, V> resultMap = MAPPER.readValue(jsonData, javaType);
return resultMap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}