【问题标题】:Unrecognized field when mapping from Firebase data structure to JAVA object从 Firebase 数据结构映射到 JAVA 对象时出现无法识别的字段
【发布时间】:2016-08-09 18:00:10
【问题描述】:

这是我试图映射到 JAVA 对象的数据结构,但我不知道如何正确映射 groupId 属性。 有人可以告诉我它是如何完成的吗? [

    Caused by: java.lang.IllegalArgumentException: Unrecognized field "1" (class firebasetest.model.GroupWithGroupId), not marked as ignorable (6 known properties: , "lastGroupId", "hourLimit", "maxLimit", "minLimit", "groupId", "actualLimit"])
    08-09 19:04:43.130 8259-8259/firebasetest I/System.out:     at [Source: N/A; line: -1, column: -1] (through reference chain: firebasetest.model.GroupWithGroupId["1"])
08-09 19:04:43.130 8259-8259/firebasetest I/System.out:     at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:2615)

和 Java 对象:

  public class GroupWithGroupId {
        public long actualLimit, hourLimit,lastGroupId,maxLimit,minLimit;
        public Map<String, Object> groupId = new HashMap<>();
        private GroupWithGroupId(){}

        public GroupWithGroupId(final long actualLimit, final long hourLimit, final long lastGroupId, final long maxLimit, final long minLimit) {
            this.actualLimit = actualLimit;
            this.hourLimit = hourLimit;
            this.lastGroupId = lastGroupId;
            this.maxLimit = maxLimit;
            this.minLimit = minLimit;
        }

        @Exclude
        public Map<String, Object> toMap() {
            HashMap<String, Object> result = new HashMap<>();
            result.put("actualLimit", actualLimit);
            result.put("hourLimit", hourLimit);
            result.put("lastGroupId", lastGroupId);
            result.put("minLimit", minLimit);
            result.put("maxLimit", maxLimit);
            result.put("groupId", groupId);
            return result;
        }
}

【问题讨论】:

    标签: android json firebase firebase-realtime-database


    【解决方案1】:

    您的班级似乎缺少@IgnoreExtraProperties

    尝试以下方法(请注意,我尚未对此进行测试,但希望它会有所帮助!):

    @IgnoreExtraProperties
    public class User {
    
            @IgnoreExtraProperties
            public class Location {
    
                    public Double latitude;
                    public Double longitude;
    
                    public Location() {}
    
                    public Location(Double latitude, Double longitude) {
                            this.latitude = latitude;
                            this.longitude = longitude;
                    }
    
                    @Exclude
                    public Map<String, Object> toMap() {
                            HashMap<String, Object> result = new HashMap<>();
                            result.put("latitude", latitude);
                            result.put("longitude", longitude);
                            return result;
                    }
            }
    
            public String firstName;
            public String lastName;
            public String email;
            public String phone;
            public Location location;
            public Date lastUpdated;
            public Date signUpDate;
    
            public User() {}
    
            public User(String firstName, String lastName, String email) {
                    this.firstName = firstName;
                    this.lastName = lastName;
                    this.email = email;
                    this.lastUpdated = new Date();
                    this.signUpDate = new Date();
            }
    
            @Exclude
            public Map<String, Object> toMap() {
                    HashMap<String, Object> result = new HashMap<>();
                    result.put("firstName", firstName);
                    result.put("lastName", lastName);
                    result.put("email", email);
                    result.put("phone", phone);
                    result.put("location", location.toMap());
                    result.put("lastUpdated", lastUpdated);
                    result.put("signUpDate", signUpDate);
                    return result;
            }
    
    }
    

    【讨论】:

    • 感谢您的建议。它有所帮助,但我必须修改一下 toMap() 函数才能正常运行。请参阅下面的解决方案
    【解决方案2】:

    我不得不修改 GroupWithoutGroupId 中的 toMap() 函数,现在它可以正常工作了!

    @IgnoreExtraProperties
    public class GroupWithoutGroupId {
        public long actualLimit, hourLimit,lastGroupId,maxLimit,minLimit;
        public GroupID groupID;
        public String groupIDkey;
    
        private GroupWithoutGroupId(){}
        public GroupWithoutGroupId(final long actualLimit, final long hourLimit, final long lastGroupId, final long maxLimit, final long minLimit) {
            this.actualLimit = actualLimit;
            this.hourLimit = hourLimit;
            this.lastGroupId = lastGroupId;
            this.maxLimit = maxLimit;
            this.minLimit = minLimit;
        }
    
        @Exclude
        public Map<String, Object> toMap() {
            HashMap<String, Object> result = new HashMap<>();
            result.put("actualLimit", actualLimit);
            result.put("hourLimit", hourLimit);
            result.put("lastGroupId", lastGroupId);
            result.put("minLimit", minLimit);
            result.put("maxLimit", maxLimit);
            result.put(groupIDkey, groupID.toMap());
            return result;
        }
    
        @IgnoreExtraProperties
        public class GroupID {
            public long sum;
            public Map<String, Object> users = new HashMap<>();
    
            public GroupID(){}
            public GroupID( final long sum, final Map<String, Object> users) {
                this.sum = sum;
                this.users = users;
            }
    
            @Exclude
            public Map<String, Object> toMap() {
                HashMap<String, Object> result = new HashMap<>();
                result.put("sum", sum);
                result.put("users", users);
    
    
           return result;
                }
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-14
      • 2018-04-02
      • 1970-01-01
      相关资源
      最近更新 更多