SzabK SzabK - 1 year ago 166
JSON Question

Unrecognized field when mapping from Firebase data structure to JAVA object

Here is the data structure which I am trying to map to a JAVA object but I can't figure it out how to properly map the groupId property.
Can someone tell me how it is done?
[roupWithGroupId firebase datastructure

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(

and the Java object:

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;

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;

Answer Source

Looks like you're missing @IgnoreExtraProperties from your class.

Try something like the following (note I haven't tested this but hopefully it will help!):

public class User {

        public class Location {

                public Double latitude;
                public Double longitude;

                public Location() {}

                public Location(Double latitude, Double longitude) {
                        this.latitude = latitude;
                        this.longitude = longitude;

                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;
       = email;
                this.lastUpdated = new Date();
                this.signUpDate = new Date();

        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;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download