我有这样的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类?
答案 0 :(得分:1)
我建议您将此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
}
]
}
以下是生成的类,我通过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 + "]";
}
}
这是代表每个成员的类:
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": {
}
}
,结果是以下输出:
结果:{2018-01-01-20-00 =示例[捐款= 4070,memberCount = 50, members = [Member [clanRank = 1,crown = 0,donations = 58,name = Min, tag = L88P2282,trophies = 4610],成员[clanRank = 2,crowns = 0, 捐款= 76,姓名= matiz,tag = G0JVYY0,trophies = 4443]]], 2018-01-02-00-00 =示例[donations = null,memberCount = null, 成员=空]}
然后,您可以根据需要在Map
中iterate over the entries。