0

I am trying to parse data from a node of firebase, which contain this json value here is pic of json of node

and corresponding data model i have created is look like this

public class Extreme {

    @SerializedName("metaData")
    public ThreadModel.MetaData metaData;
    @SerializedName("users")
    public List<Users> usersList;
    @SerializedName("messages")
    public List<MessagesTwo> messagesList;


    public static class MetaData{
        @SerializedName("createBy")
        public String createBy;
        @SerializedName("createdAt")
        public Long createdAt;
        @SerializedName("name")
        public String name;
        @SerializedName("type")
        public String type;
        @SerializedName("threadPic")
        public String threadPic;
    }

    public static class Users{
        public String key;
        public boolean value;
    }
}   

but here is the problem when i try to parse the data from firebase database snapshot it only parse metaData's value to model, all other thing just went ignore. I just want to know what should be the corresponding model look alike. It happens to me often which makes me to put this question on stack overflow. Sorry if there is any duplication of this question..

Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
JOt
  • 17
  • 7
  • it parsed metadata but didnt parse messages and users right? – Peter Haddad Nov 09 '17 at 13:03
  • The diagram is a bit obscure, but it seems like 'users' is a model with two boolean variables rather than a list. Can you provide an actual JSON string example to examine? – Neria Nachum Nov 09 '17 at 13:06
  • @PeterHadda metaData is parsed only with above model. userList & messageList remain empty which i believe a list in actual json – JOt Nov 10 '17 at 03:57
  • @PeterHaddad you have suggested to add corresponding annotations, that is not the problem my friend, i have already defined those with SerializedName. If you look closely "messages" & "users" node contains a list of records(nodes), I am defining those nodes as a List in POJO which causing me trouble here. I think i should try with Maps as suggested in other answer. – JOt Nov 10 '17 at 05:27
  • If you read my answer again you can see that firebase has built in serialized capabilities, the propertyname is used in firebase if you have a different name as field and it is why the data of users and message are not appearing. The variablename `metaData` is the same as in the database, but `userslist` and `messagelists` are not. So either change thier names to the one in databases( `users` and `messages`) and your data will appear or add propertyname with the corresponding database name inside parenthesis. Change userslist to `users` and it will be parse the values to model @JOt – Peter Haddad Nov 10 '17 at 07:05
  • Okay @PeterHaddad let me try this – JOt Nov 10 '17 at 08:01

1 Answers1

0

The model should be like:

public class Extreme {

    @SerializedName("metaData")
    public ThreadModel.MetaData metaData;
    @SerializedName("users")
    public Map<String,Boolean> usersList;
    @SerializedName("messages")
    public Map<String,Message> messagesList;

    public static class Message {
      private String id;
      private boolean seen;
      private String text;
      private long timeStamp;
      private String type;
}

You can put the name you want to the attributes inside Message but if you do, then use @SerializeName

Do not forget about getter's and setter's or make the attributes public otherwise.

The main reason of using Map is because the keys seem to be random. Because of that you can not use defined attributes.

Bonus point: You just really need @SerializedName if the name of the attributes are different from the json model.

Leandro Ocampo
  • 1,894
  • 18
  • 38
  • @Leondro Ocampo, thanks for replying I found the problem as have made some changes according to answer & it's working . thanks a lot – JOt Nov 10 '17 at 09:47
  • You're welcome. Keep in mind that some comments say that you should use PropertyName instead of SerializedName for Firebase. Maybe this link may be helpful: https://stackoverflow.com/questions/40690641/naming-convention-with-firebase-serialization-deserialization – Leandro Ocampo Nov 10 '17 at 10:22