Search code examples
javajsonretrofitpojo

How to get index of a json and use it in pojo class


I have a JSON like this. I am using retrofit to get it. the first index is the time that comes from a website and updates every 4 hours.

this is my json :

  {
        "2018-01-01-20-00": {
            "donations": 4070,
            "memberCount": 50,
            "members": [
                {
                    "clanRank": 1,
                    "crowns": 0,
                    "donations": 58,
                    "name": "Min",
                    "tag": "L88P2282",
                    "trophies": 4610
                },
                {
                    "clanRank": 2,
                    "crowns": 0,
                    "donations": 76,
                    "name": "matiz",
                    "tag": "G0JVYY0",
                    "trophies": 4443
                },
            ]
        },
        "2018-01-02-00-00": {
        },
    }

as you can see the time index is Variable(changeable).

how can I make POJO class for this JSON?


Solution

  • I would recommend that you parse this JSON as a Map<String,SomeExampleClass> where SomeExampleClass is a POJO that you create to represent the objects associated with each of the dates.

    For example I used the inner portion of your JSON on jsonschema2pojo (I selected gson as the annotation style) and generated POJO classes.

    Here is the portion of the JSON:

    {
        "donations": 4070,
        "memberCount": 50,
        "members": [
            {
                "clanRank": 1,
                "crowns": 0,
                "donations": 58,
                "name": "Min",
                "tag": "L88P2282",
                "trophies": 4610
            },
            {
                "clanRank": 2,
                "crowns": 0,
                "donations": 76,
                "name": "matiz",
                "tag": "G0JVYY0",
                "trophies": 4443
            }
        ]
    }
    

    Here are the generated classes, with a toString method I added via Eclipse:

    package com.example;
    
    import java.util.List;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Example {
    
        @SerializedName("donations")
        @Expose
        private Integer donations;
        @SerializedName("memberCount")
        @Expose
        private Integer memberCount;
        @SerializedName("members")
        @Expose
        private List<Member> members = null;
    
        public Integer getDonations() {
            return donations;
        }
    
        public void setDonations(Integer donations) {
            this.donations = donations;
        }
    
        public Integer getMemberCount() {
            return memberCount;
        }
    
        public void setMemberCount(Integer memberCount) {
            this.memberCount = memberCount;
        }
    
        public List<Member> getMembers() {
            return members;
        }
    
        public void setMembers(List<Member> members) {
            this.members = members;
        }
    
        @Override
        public String toString() {
            return "Example [donations=" + donations + ", memberCount=" + memberCount + ", members=" + members + "]";
        }
    
    }
    

    This is the class representing each member:

    package com.example;
    
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Member {
    
        @SerializedName("clanRank")
        @Expose
        private Integer clanRank;
        @SerializedName("crowns")
        @Expose
        private Integer crowns;
        @SerializedName("donations")
        @Expose
        private Integer donations;
        @SerializedName("name")
        @Expose
        private String name;
        @SerializedName("tag")
        @Expose
        private String tag;
        @SerializedName("trophies")
        @Expose
        private Integer trophies;
    
        public Integer getClanRank() {
            return clanRank;
        }
    
        public void setClanRank(Integer clanRank) {
            this.clanRank = clanRank;
        }
    
        public Integer getCrowns() {
            return crowns;
        }
    
        public void setCrowns(Integer crowns) {
            this.crowns = crowns;
        }
    
        public Integer getDonations() {
            return donations;
        }
    
        public void setDonations(Integer donations) {
            this.donations = donations;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getTag() {
            return tag;
        }
    
        public void setTag(String tag) {
            this.tag = tag;
        }
    
        public Integer getTrophies() {
            return trophies;
        }
    
        public void setTrophies(Integer trophies) {
            this.trophies = trophies;
        }
    
        @Override
        public String toString() {
            return "Member [clanRank=" + clanRank + ", crowns=" + crowns + ", donations=" + donations + ", name=" + name
                    + ", tag=" + tag + ", trophies=" + trophies + "]";
        }
    
    }
    

    Now here is some example code to parse the JSON as a Map:

    package gson;
    
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.util.Map;
    
    import com.example.Example;
    import com.google.gson.Gson;
    import com.google.gson.reflect.TypeToken;
    
    public class GsonMain {
    
        public static void main(String[] args) throws IOException {
            Gson gson = new Gson();
            byte[] jsonBytes = Files.readAllBytes(Paths.get("./src/main/java/gson/jsonData.json"));
            String json = new String(jsonBytes);
    
            Map<String,Example> result = gson.fromJson(json, new TypeToken<Map<String, Example>>(){}.getType());
    
            System.out.println("RESULTS: "+result);
        }
    
    }
    

    To test this I used the following JSON input:

    {
            "2018-01-01-20-00": {
                "donations": 4070,
                "memberCount": 50,
                "members": [
                    {
                        "clanRank": 1,
                        "crowns": 0,
                        "donations": 58,
                        "name": "Min",
                        "tag": "L88P2282",
                        "trophies": 4610
                    },
                    {
                        "clanRank": 2,
                        "crowns": 0,
                        "donations": 76,
                        "name": "matiz",
                        "tag": "G0JVYY0",
                        "trophies": 4443
                    }
                ]
            },
            "2018-01-02-00-00": {
            }
        }
    

    and the result was the following output:

    RESULTS: {2018-01-01-20-00=Example [donations=4070, memberCount=50, members=[Member [clanRank=1, crowns=0, donations=58, name=Min, tag=L88P2282, trophies=4610], Member [clanRank=2, crowns=0, donations=76, name=matiz, tag=G0JVYY0, trophies=4443]]], 2018-01-02-00-00=Example [donations=null, memberCount=null, members=null]}

    You can then iterate over the entries in the Map as needed.