gpt4 book ai didi

java - 如何获取json的索引并在pojo类中使用它

转载 作者:行者123 更新时间:2023-12-02 11:06:48 25 4
gpt4 key购买 nike

我有一个像这样的 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 类?

最佳答案

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

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

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

这是生成的类,带有 toString我通过 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 + "]";
}

}

这是代表每个成员的类:

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

然后您可以 iterate over the entriesMap根据需要。

关于java - 如何获取json的索引并在pojo类中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50897610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com