0

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?

Saheb
  • 1,392
  • 3
  • 13
  • 33
Hadi Ahmadi
  • 129
  • 12
  • 1
    learn to google, its an amazing skill, you are looking for something like this http://www.jsonschema2pojo.org/ – Ezio Jun 17 '18 at 14:38
  • unfortunately this site can not help me . because this site just make pojo . but i dont want to know how to create pojo i know it . i just want to know how to get index and use it.@Ezio – Hadi Ahmadi Jun 17 '18 at 14:40
  • You need to create a List of POJOs, so you can get the item with `List.get(index);` If this time variables will be dynamic I recommend use a JSON array instead of JSON objects – Marc Estrada Jun 17 '18 at 14:57
  • thanks . you mean its better to change the json before sending it ? – Hadi Ahmadi Jun 17 '18 at 15:09

1 Answers1

1

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.

D.B.
  • 4,523
  • 2
  • 19
  • 39
  • thanks . you explained me basics and i've done what is in my mind by your solution . perfect !!! – Hadi Ahmadi Jun 20 '18 at 17:23
  • if you know any training video about maps that could be useful for me i would be happy to know . its not matter if that come from youtube or anywhere . i just want to be professional in working with map !!! – Hadi Ahmadi Jun 20 '18 at 17:28
  • `Map` is part of the [Java Collections Framework](https://docs.oracle.com/javase/tutorial/collections/) and that is where you should probably begin learning about them. – D.B. Jun 20 '18 at 19:14