如何获取 json 的索引并在 pojo class 中使用它

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

我有一个 JSON 这样的。 我正在使用改造来获得它。 第一个索引是来自网站的时间,每 4 小时更新一次。

这是我的 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": {
        },
    }

如您所见,时间索引是可变的(可变的)。

如何为此 JSON 制作 POJO class?

我建议您将此 JSON 解析为 Map<String,SomeExampleClass>,其中 SomeExampleClass 是您创建的 POJO,用于表示与每个日期关联的对象。

例如,我在 jsonschema2pojo 上使用了你 JSON 的内部部分(我选择 gson 作为注释样式)并生成了 POJO classes。

这是 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
        }
    ]
}

这里是生成的 classes,带有我通过 Eclipse 添加的 toString 方法:

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 + "]";
    }

}

这是class代表每个成员:

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 + "]";
    }

}

下面是一些将 JSON 解析为 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);
    }

}

为了测试这一点,我使用了以下 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": {
        }
    }

结果是以下输出:

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]}

然后您可以根据需要在 Mapiterate over the entries