json序列化定义类型jsckson

可以解决一批问题。

1.导包

 <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.7</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.7</version> </dependency>

2. json反序列化定义

package javastu;import com.fasterxml.jackson.core.JsonParser;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.DeserializationContext;import com.fasterxml.jackson.databind.JsonDeserializer;import java.io.IOException;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class DateJsonDeserializer extends JsonDeserializer<Date> { private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { try { return simpleDateFormat.parse(jsonParser.getText()); } catch (ParseException e) { e.printStackTrace(); } return null; }}

3. 自定义json序列化类

package javastu;import com.fasterxml.jackson.core.JsonGenerator;import com.fasterxml.jackson.databind.JsonSerializer;import com.fasterxml.jackson.databind.SerializerProvider;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;public class DateJsonSerializer extends JsonSerializer<Date> { public static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeString(simpleDateFormat.format(date)); }}

4.对象

package javastu;import com.fasterxml.jackson.databind.annotation.JsonDeserialize;import com.fasterxml.jackson.databind.annotation.JsonSerialize;import java.util.Date;public class Student { private String name; @JsonDeserialize(using = DateJsonDeserializer.class) @JsonSerialize(using = DateJsonSerializer.class) private Date time; public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getTime() { return time; } public void setTime(Date time) { this.time = time; } @Override public String toString() { return "Student{" + "name=‘" + name + ‘\‘‘ + ", time=" + time + ‘}‘; }}

6. 测试类

package javastu;import com.fasterxml.jackson.databind.ObjectMapper;import java.io.IOException;import java.util.Date;public class App { public static void main(String[] args) throws IOException { Student student = new Student(); student.setName("张三"); student.setTime(new Date()); ObjectMapper objectMapper = new ObjectMapper(); String s = objectMapper.writeValueAsString(student); Student student2 = objectMapper.readValue(s, Student.class); System.out.println(s); System.out.println(student2); }}

 

相关文章