我有这门课:
public static class Person {
public Person(){
}
public String get_id() {
return _id;
}
public int getIndex() {
return index;
}
public String getGuid() {
return guid;
}
public boolean isActive() {
return isActive;
}
public String getBalance() {
return balance;
}
public String getPicture() {
return picture;
}
public int getAge() {
return age;
}
public String getEyeColor() {
return eyeColor;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public String getCompany() {
return company;
}
public String getEmail() {
return email;
}
public String getPhone() {
return phone;
}
public String getAddress() {
return address;
}
public String getAbout() {
return about;
}
public String getRegistered() {
return registered;
}
public double getLatitude() {
return latitude;
}
public double getLongitude() {
return longitude;
}
public List<String> getTags() {
return tags;
}
public List<App.Friend> getFriends() {
return friends;
}
public String getGreeting() {
return greeting;
}
public String getFavoriteFruit() {
return favoriteFruit;
}
private boolean isActive;
private String _id;
private int index;
private String guid;
private String balance;
private String picture;
private int age;
private String eyeColor;
private String name;
private String gender;
private String company;
private String email;
private String phone;
private String address;
private String about;
private String registered;
private double latitude;
private double longitude;
private List<String> tags;
private List<App.Friend> friends;
private String greeting;
private String favoriteFruit;
@Override
public String toString() {
return "Person{" +
"_id='" + _id + '\'' +
", index=" + index +
", guid='" + guid + '\'' +
", isActive=" + isActive +
", balance='" + balance + '\'' +
", picture='" + picture + '\'' +
", age=" + age +
", eyeColor='" + eyeColor + '\'' +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", company='" + company + '\'' +
", email='" + email + '\'' +
", phone='" + phone + '\'' +
", address='" + address + '\'' +
", about='" + about + '\'' +
", registered='" + registered + '\'' +
", latitude=" + latitude +
", longitude=" + longitude +
", tags=" + tags +
", friends=" + friends +
", greeting='" + greeting + '\'' +
", favoriteFruit='" + favoriteFruit + '\'' +
'}';
}
}
我正在尝试使用 Jackson ObjectMapper 读取包含对象数组的 JSON,一切正常,但当它必须读取字段“isActive”时,它会抛出异常“com.fasterxml.jackson.databind .exc.UnrecognizedPropertyException:无法识别的字段“isActive”(类 com.jcg.maven.App$Person),未标记为可忽略(21 个已知属性:“性别”、“已注册”、“guid”、“名称”、“纬度” ", "about", "phone", "longitude", "index", "address", "friends", "favoriteFruit", "tags", "_id", "balance", "company", "picture", “年龄”、“眼睛颜色”、“问候语”、“电子邮件”])
在 [来源:generated.json;行:6,列:22](通过引用链:java.util.ArrayList[0]->com.jcg.maven.Person["isActive"])
我不想让它被忽略,因为我会失去它的值(value),我的 JSON 文件中的字段也是这样写的:
some fields
"isActive": false,
other fields
所以我排除了格式错误的json文件的可能性
最佳答案
正如评论中所建议的那样,解决方案非常简单,我只需将 isActive
字段的 getter 和 setter 方法的名称更改为 getIsActive
和 setIsActive
(IntelliJ IDEA 使用不同的名称自动创建了这些方法
关于java - jackson 无法识别的领域,即使它在类里面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49107370/