brief introduction
ObjectMapper class (com.fasterxml.jackson.databind.ObjectMapper) yes Jackson Main classes of , It can help us quickly carry out various types and tasks Json Mutual conversion of types .
use
1, introduce Jackson Dependence of
<!-- Introduce relevant version dependencies according to your needs . --> <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId> <version>2.9.10</version> </dependency>
<dependency> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId> <version>2.9.10</version>
</dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId> <version>2.9.10</version>
</dependency>
2,ObjectMapper Common configurations for
private static final ObjectMapper mapper; public static ObjectMapper
getObjectMapper(){ return this.mapper; } static{ // establish ObjectMapper object mapper =
new ObjectMapper() //configure method Configure some required parameters // Convert to formatted json Displayed format beautification
mapper.enable(SerializationFeature.INDENT_OUTPUT); // The attributes of the sequence object when serializing
//JsonInclude.Include.NON_DEFAULT Property is the default value and is not serialized //JsonInclude.Include.ALWAYS All properties
//JsonInclude.Include.NON_EMPTY Attribute is empty (“”) Or for NULL No serialization
//JsonInclude.Include.NON_NULL Attribute is NULL No serialization
mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
// When deserializing , Will an error be reported when an unknown attribute is encountered //true - An error will be reported if there is no attribute false - No properties will be managed , No error will be reported
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// If it is an empty object , No throw exception mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,
false); // ignore transient Decorated properties
mapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true); // Modify serialized date format
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
// Handling different time zone offset formats mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.registerModule(new JavaTimeModule()); }
3,ObjectMapper Common methods of
a.json String to object
ObjectMapper mapper = new ObjectMapper(); String jsonString =
"{\"name\":\"Hyl\", \"age\":20}"; // Convert string to object Student student =
mapper.readValue(jsonString, Student.class); System.out.println(student);
// Convert objects to json character string jsonString = mapper.writeValueAsString(student);
System.out.println(jsonString); result : Student [ name: Hyl, age: 20 ] { "name" :
"Hyl", "age" : 20 }
b. Conversion between arrays and objects
// Object to byte array byte[] byteArr = mapper.writeValueAsBytes(student);
System.out.println(byteArr); //byte Convert array to object Student student=
mapper.readValue(byteArr, Student.class); System.out.println(student); result :
[B@3327bd23 Student [ name: Hyl, age: 20 ]
c. Set sum json Conversion between strings
List<Student> studentList= new ArrayList<>(); studentList.add(new
Student("hyl1" ,20 , new Date())); studentList.add(new Student("hyl2" ,21 , new
Date())); studentList.add(new Student("hyl3" ,22 , new Date()));
studentList.add(new Student("hyl4" ,23 , new Date())); String jsonStr =
mapper.writeValueAsString(studentList); System.out.println(jsonStr);
List<Student> studentList2 = mapper.readValue(jsonStr, List.class);
System.out.println(" String to set :" + studentList2 ); result : [ { "name" : "hyl1", "age" :
20, "sendTime" : 1525164212803 }, { "name" : "hyl2", "age" : 21, "sendTime" :
1525164212803 }, { "name" : "hyl3", "age" : 22, "sendTime" : 1525164212803 }, {
"name" : "hyl4", "age" : 23, "sendTime" : 1525164212803 } ] [{name=hyl1,
age=20, sendTime=1525164212803}, {name=hyl2, age=21, sendTime=1525164212803},
{name=hyl3, age=22, sendTime=1525164212803}, {name=hyl4, age=23,
sendTime=1525164212803}]
d. map and json Conversion between strings
Map<String, Object> testMap = new HashMap<>(); testMap.put("name", "22");
testMap.put("age", 20); testMap.put("date", new Date()); testMap.put("student",
new Student("hyl", 20, new Date())); String jsonStr =
mapper.writeValueAsString(testMap); System.out.println(jsonStr); Map<String,
Object> testMapDes = mapper.readValue(jsonStr, Map.class);
System.out.println(testMapDes); result : { "date" : 1525164212803, "name" : "22",
"student" : { "name" : "hyl", "age" : 20, "sendTime" : 1525164212803, "intList"
: null }, "age" : 20 } {date=1525164212803, name=22, student={name=hyl, age=20,
sendTime=1525164212803, intList=null}, age=20}
e. Date transfer json character string
// Modify time format mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
Student student = new Student ("hyl",21, new Date());
student.setIntList(Arrays.asList(1, 2, 3)); String jsonStr =
mapper.writeValueAsString(student); System.out.println(jsonStr); result : { "name" :
"hyl", "age" : 21, "sendTime" : "2020-07-23 13:14:36", "intList" : [ 1, 2, 3 ] }
js Convert string to json object
var data = "{\"name\":\"Hyl\", \"age\":20}"; var student = eval(data);
console.info(student.name); console.info(student.age); result : Hyl 20
Technology