java - jackson 无法识别的领域,即使它在类里面

标签 java json file jackson

我有这门课:

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 方法的名称更改为 getIsActivesetIsActive(IntelliJ IDEA 使用不同的名称自动创建了这些方法

关于java - jackson 无法识别的领域,即使它在类里面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49107370/

相关文章:

java - 我可以将自己的类数据保存到文件中吗?

java - 使用 JPA 从子对象级联到父对象

java - Hibernate envers 在没有更改时创建记录

file - 在 Emacs 中打开文件的速度非常快,类似于 Sublime 的 Goto Anything

c - 在 C 中将数字从文件保存到数组

java - 多线程 - 如何安全地从 ArrayList 中删除?

mysql - 我需要使用mysql中json数据列的索引

java - 在 Java 中将字符串值传递给 Jackson Custom Deserializer

json - 如何在powershell中选择具有特殊字符的键?

file - 尽管文件夹已被删除,但移动项目会导致 "File Already Exists"错误