<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
</dependency>
public static void main(String[] args) throws Exception{
JSONObject jo = new JSONObject();
jo.put("Test", "aaa");
jo.put("Date", "2020-01-15");
String json = jo.toJSONString();
ObjectMapper gson = new ObjectMapper();
// 转换为格式化的json
gson.enable(SerializationFeature.INDENT_OUTPUT);
// 如果json中有新增的字段并且是实体类类中不存在的,不报错
gson.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Aa aa= new ObjectMapper().readValue(json, Aa.class);
System.out.println(aa);
System.out.println(new ObjectMapper().writeValueAsString(aa));
}
public static class Aa{
@JsonProperty("Test")
private String test;
@JsonProperty("Date")
private String date;
@JsonIgnore
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
@JsonIgnore
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}