我想知道如何在Java-POJO结构中表示JSON文件的这一部分。 JSON文件本身具有我以前从未见过的结构:
"codeShares": [
{
"codeShare": {
"airlineCode": "LH",
"trackNumber": "9695",
"suffix": ""
}
},
{
"codeShare": {
"airlineCode": "NH",
"trackNumber": "5350",
"suffix": ""
}
},
{
"codeShare": {
"airlineCode": "TP",
"trackNumber": "8988",
"suffix": ""
}
},
{
"codeShare": {
"airlineCode": "UA",
"trackNumber": "7212",
"suffix": ""
}
}
]
如果没有那些" codeShare"每次都标记一次"正常"数组,我理解如何在POJO中表示它,但在我测试时,List不起作用。列表的大小与" codeShare"的数量相匹配。文件中的标签,但实际值如" airlineCode"等等总是空的。如果它不是List<>,我该怎么办?
共享代码级:
private String airlineCode;
private String trackNumber;
private String suffix;
public CodeShare(String airlineCode, String trackNumber, String suffix) {
this.airlineCode = airlineCode;
this.trackNumber = trackNumber;
this.suffix = suffix;
}
public String getAirlineCode() {
return airlineCode;
}
public String getTrackNumber() {
return trackNumber;
}
public String getSuffix() {
return suffix;
}
@Override
public String toString() {
return airlineCode + trackNumber;
}
Kenta1561
答案 0 :(得分:0)
创建另一个包含codeShare对象的类。
E.g。
public class CodeShareWrapper {
public CodeShare codeShare;
public CodeShareWrapper() {}
}
现在响应类将为:
public class MyResponse {
public List<CodeShareWrapper> codeShares;
public MyResponse() {}
}
将MyResponse与Retrofit一起使用,或在使用Gson
反序列化JSON响应时使用。
MyResponse = new Gson().fromJson(jsonString, MyResponse.class);