在不改变POJO的情况下,将不区分大小写的JSON转换为POJO映射 [英] Case insensitive JSON to POJO mapping without changing the POJO
问题描述
JSON-String:
[{FIRSTNAME:John,LASTNAME:Doe,DATEOFBIRTH:1980-07-16T18:25: 00.000Z}]
POJO类:
public class Person {
private String firstName;
private String lastName;
private Date dateOfBirth;
public String getFirstName(){
return firstName;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getLastName(){
return lastName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public Date getDateOfBirth(){
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth){
this.dateOfBirth = dateOfBirth;
}
}
测试类:
@Test
public final void testDeserializingPersonJsonToPersonClass()
throws JsonParseException,JsonMappingException,IOException {
final String jsonAsString =[ {\FIRSTNAME\:\John\,\LASTNAME\:\Doe\,\DATEOFBIRTH\:\1980-07-16T18: 25:00.000Z\}]
final ObjectMapper mapper = new ObjectMapper();
final Person person = mapper.readValue(jsonAsString,Person.class);
assertNotNull(person);
assertThat(person.getFirstName(),equalTo(John));
}
最终会出现以下错误:
com。 quickxml.jackson.databind.JsonMappingException:无法反序列化...
不可以更改JSON-String和POJO-Class。
我也有同样的问题,找不到全局解决方法。但是,您可以为每个属性设置2个setter:
@JsonSetter(FIRSTNAME)
public void setFirstNameCaps (String firstName){
this.firstName = firstName;
}
@JsonSetter(firstName)
public void setFirstName(String firstName){
this.firstName = firstName;
}
不优雅,但适用于大小写的json字段。您也可以尝试此处所提及的解决方案,但这可能会导致性能开销
Does anyone know how com.fasterxml.jackson.databind.ObjectMapper is able to map JSON properties to POJO properties case insensitive?
JSON-String:
[{"FIRSTNAME":"John","LASTNAME":"Doe","DATEOFBIRTH":"1980-07-16T18:25:00.000Z"}]
POJO-Class:
public class Person {
private String firstName;
private String lastName;
private Date dateOfBirth;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
}
Test-Class:
@Test
public final void testDeserializingPersonJsonToPersonClass()
throws JsonParseException, JsonMappingException, IOException {
final String jsonAsString = "[{\"FIRSTNAME\":\"John\",\"LASTNAME\":\"Doe\",\"DATEOFBIRTH\":\"1980-07-16T18:25:00.000Z\"}]";
final ObjectMapper mapper = new ObjectMapper();
final Person person = mapper.readValue(jsonAsString, Person.class);
assertNotNull(person);
assertThat(person.getFirstName(), equalTo("John"));
}
This ends up in following error:
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of ...
It's not possible to change neither JSON-String nor POJO-Class.
I had the same problem and couldn't find a global way of solving this. However you can have 2 setters per property to achieve this:
@JsonSetter("FIRSTNAME")
public void setFirstNameCaps(String firstName) {
this.firstName = firstName;
}
@JsonSetter("firstName")
public void setFirstName(String firstName) {
this.firstName = firstName;
}
Not elegant but will work for both upper and lower case json fields. You can also try the solution mentioned here but this might have a performance overhead
这篇关于在不改变POJO的情况下,将不区分大小写的JSON转换为POJO映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!