即使是在课堂上,杰克逊也没有认识到这个领域

时间:2018-03-05 09:43:23

标签: 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: Unrecognized field "isActive" (class com.jcg.maven.App$Person), not marked as ignorable (21 known properties: "gender", "registered", "guid", "name", "latitude", "about", "phone", "longitude", "index", "address", "friends", "favoriteFruit", "tags", "_id", "balance", "company", "picture", "age", "eyeColor", "greeting", "email"]) at [Source: generated.json; line: 6, column: 22] (through reference chain: java.util.ArrayList[0]->com.jcg.maven.Person["isActive"])

我不想让它忽略,因为我会失去它的价值,我的JSON文件中的字段也是这样写的:

 some fields
"isActive": false,
 other fields

所以我排除了格式错误的json文件的可能性

2 个答案:

答案 0 :(得分:2)

正如评论中所建议的那样,解决方案非常简单,我只需要将isActive字段的getter和setter方法的名称更改为getIsActivesetIsActive(IntelliJ) IDEA使用不同的名称

自动创建这些方法

答案 1 :(得分:1)

创建getter&amp;使用IDE生成选项的setter将创建具有不同名称的getter和setter。(我在eclipse中经历过)

boolean isActive;

public boolean isActive() {
    return isActive;
}

public void setActive(boolean isActive) {
    this.isActive = isActive;
}

当我们在变量中使用 isActive isDeleted 类型的名称时,可能会出现此问题。

为此类型的字段手动创建getter setter以解决问题。

    getIsActive(); 
    setIsActive();