Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Firebase stored "array" object to POJO

I am trying to retrieve data from firebase in a recyclerview in my android application. The object at the "boardz" node I am referencing has 2 child properties. Those properties each have multiple child properties.

Updated with Firebase json and pojo:

{
"game" : {
"boardz" : {
  "-KTaL6V06e3SkEAvfU_3" : {
    "hostData" : {
      "hostId" : "MUC6gtRqInRQmqxe7khlmrAbjVZ2",
      "ownerAddress" : "n3b9t3vDR6Yiot4HwxwZoa7vxZBiAKReeH",
      "secretString" : "derp-a-derp"
    },
    "metadata" : {
      "across" : [ 2, 0, 5, 9, 7, 8, 1, 4, 3, 6 ],
      "awayTeam" : "ATL",
      "boardFeeBTC" : "",
      "boardFeeUSD" : 14,
      "collectionAddress" : "2NEPnGPyvy9F5N4W4CjhLNPZLwTFNSAzjAL",
      "down" : [ 1, 6, 8, 7, 3, 5, 2, 0, 9, 4 ],
      "endTime" : "2016-10-09T22:09:39.807Z",
      "homeTeam" : "DEN",
      "q1AwayScore" : "",
      "q1HomeScore" : "",
      "q1WinnerAddress" : "",
      "q2AwayScore" : "",
      "q2HomeScore" : "",
      "q2WinnerAddress" : "",
      "q3AwayScore" : "",
      "q3HomeScore" : "",
      "q3WinnerAddress" : "",
      "q4AwayScore" : "",
      "q4HomeScore" : "",
      "q4WinnerAddress" : "",
      "startTime" : "2016-10-08T22:09:39.807Z",
      "winnersPaid" : false
    }
  },
  "-KTaP0-GPSCwCDA8BUlt" : {
    "hostData" : {
      "hostId" : "MUC6gtRqInRQmqxe7khlmrAbjVZ2",
      "ownerAddress" : "mtLF8hks46i9DbSK1tBpenYMDsHpTbnuwC",
      "secretString" : "do-diddly-doo"
    },
    "metadata" : {
      "across" : [ 9, 1, 6, 8, 0, 2, 4, 7, 3, 5 ],
      "awayTeam" : "BUF",
      "boardFeeBTC" : "",
      "boardFeeUSD" : 25,
      "collectionAddress" : "2N5VgMfKwCtJaiT4CLVAMvMQUG6PTbreY4z",
      "down" : [ 9, 2, 4, 3, 7, 0, 5, 6, 8, 1 ],
      "endTime" : "2016-10-09T22:26:47.467Z",
      "homeTeam" : "LA",
      "q1AwayScore" : "",
      "q1HomeScore" : "",
      "q1WinnerAddress" : "",
      "q2AwayScore" : "",
      "q2HomeScore" : "",
      "q2WinnerAddress" : "",
      "q3AwayScore" : "",
      "q3HomeScore" : "",
      "q3WinnerAddress" : "",
      "q4AwayScore" : "",
      "q4HomeScore" : "",
      "q4WinnerAddress" : "",
      "startTime" : "2016-10-08T22:26:47.467Z",
      "winnersPaid" : false
    }
  }
},

I used this answer How to represent nested data in Firebase class to create the pojo to retrieve the nested data and the classes are working partially.

The problem I have is the answer doesn't explain how to retrieve array data from the database. (not lists of Objects, just plain array data with integers) the child2 pojo isn't being populated with the data from firebase.

The hostData pojo is mapped successfully to the firebase child nodes. All of hostData's properties are Strings so I think that made it easy to match.

metaData has an array as it's first property.

I know that firebase stores arrays as json objects, as explained here. I need to convert these "array" objects into java lists or ArrayList so the pojo for metaData will be populated.

So how do you represent array data stored in a firebase node as a pojo?

I added the pojo to help see how I created the object and mapped the attributes. Maybe I missed something.

public class Board {

private HostData hostData;//this class maps to the firebase json
private MetaData metaData;//this class isn't mapping to any of the json properties.

public Board() {
}

public Board(HostData hostData, MetaData metaData) {
    this.hostData = hostData;
    this.metaData = metaData;
}

public HostData getHostData() {
    return hostData;
}

public MetaData getMetaData() {
    return metaData;
}
//the hostData class works fine
public static class HostData {

    private String hostId;
    private String ownerAddress;
    private String secretString;

    public HostData() {
    }

    public HostData(String hostId, String ownerAddress, String secretString) {
        this.hostId = hostId;
        this.ownerAddress = ownerAddress;
        this.secretString = secretString;
    }

    public String getHostId() {
        return hostId;
    }

    public String getOwnerAddress() {
        return ownerAddress;
    }

    public String getSecretString() {
        return secretString;
    }
}
//this class is giving me hell. Am I missing something?
public static class MetaData {

    private List<Long> across = new ArrayList<>();
    private String awayTeam;
    private Double boardFeeBTC;
    private Double boardFeeUSD;
    private String collectionAddress;
    private List<Long> down = new ArrayList<>();
    private String endTime;
    private String homeTeam;
    private Long q1AwayScore;
    private Long q1HomeScore;
    private String q1WinnerAddress;
    private Long q2AwayScore;
    private Long q2HomeScore;
    private String q2WinnerAddress;
    private Long q3AwayScore;
    private Long q3HomeScore;
    private String q3WinnerAddress;
    private Long q4AwayScore;
    private Long q4HomeScore;
    private String q4WinnerAddress;
    private String startTime;
    private Boolean winnersPaid;

    public MetaData() {
    }

    public MetaData(List<Long> across, String awayTeam, Double boardFeeBTC, Double boardFeeUSD, String collectionAddress, List<Long> down, String endTime, String homeTeam, Long q1AwayScore, Long q1HomeScore, String q1WinnerAddress, Long q2AwayScore, Long q2HomeScore, String q2WinnerAddress, Long q3AwayScore, Long q3HomeScore, String q3WinnerAddress, Long q4AwayScore, Long q4HomeScore, String q4WinnerAddress, String startTime, Boolean winnersPaid) {
        this.across = across;
        this.awayTeam = awayTeam;
        this.boardFeeBTC = boardFeeBTC;
        this.boardFeeUSD = boardFeeUSD;
        this.collectionAddress = collectionAddress;
        this.down = down;
        this.endTime = endTime;
        this.homeTeam = homeTeam;
        this.q1AwayScore = q1AwayScore;
        this.q1HomeScore = q1HomeScore;
        this.q1WinnerAddress = q1WinnerAddress;
        this.q2AwayScore = q2AwayScore;
        this.q2HomeScore = q2HomeScore;
        this.q2WinnerAddress = q2WinnerAddress;
        this.q3AwayScore = q3AwayScore;
        this.q3HomeScore = q3HomeScore;
        this.q3WinnerAddress = q3WinnerAddress;
        this.q4AwayScore = q4AwayScore;
        this.q4HomeScore = q4HomeScore;
        this.q4WinnerAddress = q4WinnerAddress;
        this.startTime = startTime;
        this.winnersPaid = winnersPaid;
    }

    public List<Long> getAcross() {
        return across;
    }

    public String getAwayTeam() {
        return awayTeam;
    }

    public Double getBoardFeeBTC() {
        return boardFeeBTC;
    }

    public Double getBoardFeeUSD() {
        return boardFeeUSD;
    }

    public String getCollectionAddress() {
        return collectionAddress;
    }

    public List<Long> getDown() {
        return down;
    }

    public String getEndTime() {
        return endTime;
    }

    public String getHomeTeam() {
        return homeTeam;
    }

    public Long getQ1AwayScore() {
        return q1AwayScore;
    }

    public Long getQ1HomeScore() {
        return q1HomeScore;
    }

    public String getQ1WinnerAddress() {
        return q1WinnerAddress;
    }

    public Long getQ2AwayScore() {
        return q2AwayScore;
    }

    public Long getQ2HomeScore() {
        return q2HomeScore;
    }

    public String getQ2WinnerAddress() {
        return q2WinnerAddress;
    }

    public Long getQ3AwayScore() {
        return q3AwayScore;
    }

    public Long getQ3HomeScore() {
        return q3HomeScore;
    }

    public String getQ3WinnerAddress() {
        return q3WinnerAddress;
    }

    public Long getQ4AwayScore() {
        return q4AwayScore;
    }

    public Long getQ4HomeScore() {
        return q4HomeScore;
    }

    public String getQ4WinnerAddress() {
        return q4WinnerAddress;
    }

    public String getStartTime() {
        return startTime;
    }

    public Boolean getWinnersPaid() {
        return winnersPaid;
    }
}
}
like image 466
kreshendo Avatar asked Feb 06 '23 16:02

kreshendo


2 Answers

Looks like a small typo problem. The key in the database should match (case sensitive) with the one in the POJO class.

public class Board {
    private HostData hostData;
    private MetaData metadata; // change to this

    ... constructor, getter, setter
}

or this

public class Board {
    public HostData hostData;
    @PropertyName("metadata")
    public MetaData metaData; // make sure the field is in public

    ... constructor, getter, setter
}
like image 178
Wilik Avatar answered Feb 09 '23 00:02

Wilik


Go to Settings -> Plugins -> Browse Repositories -> Serch RoboPOJOgenerator. And Follow this documentation.

like image 29
Krunal Kapadiya Avatar answered Feb 08 '23 23:02

Krunal Kapadiya