使用它作为参考我已经描述了我的Json数据的结构,并且可以根据需要获取信息,直到我得到嵌套记录和数组。 Parsing a complex Json Object using GSON in Java
但是我的JSON数据已多次嵌套。例如;
{
"meetings": [
{
"meetingName": "GaryVon",
"location": "USA",
"meetingType": "P",
"meetingDate": "2016-03-25",
"weatherCondition": "FINE",
"showCode": {
"meetingCode": "A",
"scheduledType": "R"
},
"venueType": "ANI",
"showPools": [
{
"showProduct": "GaryVon",
"showStatus": "Open",
}
]
}
]
}
我有我的包装器和描述json数据格式的类。新java文件中的每个类。
public class meetingContainer {
public List<meetings> meetings;
}
顶级课程
public class meetings {
private String meetingName;
private String location;
private String meetingType;
private String meetingDate;
private String weatherCondition;
private ShowCode showCode;
private String venueType;
private ShowPools[] showPools;
public String getMeetingName() { return meetingName; }
public String getLocation() { return location; }
public String getMeetingType() { return meetingType; }
public String getMeetingDate() { return meetingDate; }
public String getWeatherCondition() { return weatherCondition; }
public ShowCode getShowCode() { return showCode; }
public String getVenueType() { return venueType; }
public ShowPools[] getShowPools() { return showPools; }
}
二级课程
public class ShowCode {
private String meetingCode;
private String scheduledType;
public String getMeetingCode() { return meetingCode; }
public String getScheduledType() { return scheduledType; }
}
二级课程
public class ShowPools {
private String showProduct;
private String showStatus;
public String getShowProduct() { return showProduct; }
public String getShowStatus() { return showStatus; }
}
然后我尝试解析它并获取正常工作的数据,直到我进入嵌套数组/记录
Gson g = new Gson();
meetingContainer mc = g.fromJson(jsonMeetingsString, meetingContainer.class);
for(meetings m: mc.meetings){
System.out.println(m.getMeetingName()); //Result = "GaryVon"
System.out.println(m.getLocation()); //Result = "USA"
System.out.println(m.getmeetingType()); //Result = "P"
System.out.println(m.getShowCode()); //Result = "packagename.ShowCode@210366b4"
}
我的问题是如何声明嵌套数组/记录,然后从不同的类调用这些方法,即调用showcode和showpools中的方法。另一篇文章没有说明如何。很抱歉,如果这是一个简单的答案,因为我是java的新手。
答案 0 :(得分:1)
m.getShowCode().getMeetingCode()
这将返回ShowCode类型的引用,以使用getter访问内部值,例如:
private List<ShowPools> showPools;
您应该使用showPools的列表
lst = ['a', 'b', 'c']
pad = len('results:') * ' ' # Number of spaces to insert (2nd, 3rd, ... lines)
for i, x in enumerate(lst):
if i == 0:
print('results:', x)
else:
print(pad, x)
答案 1 :(得分:1)
您提供的JSON字符串无效。它有一个额外的,
-
{
"showProduct": "GaryVon",
"showStatus": "Open",
^
回答您在评论中提出的问题:m.getShowCode().getShowProduct()
无效,因为 showCode 节点只有两个属性 meetingCode 和 scheduledType
下面的代码列出了JSON的所有值。如果它没有涵盖您的问题,请告诉我
Gson g = new Gson();
meetingContainer mc = g.fromJson(jsonMeetingsString,
meetingContainer.class);
for (meetings m : mc.meetings) {
System.out.println("meetingName: " + m.getMeetingName());
System.out.println("location: "+ m.getLocation());
System.out.println("meetingType: "+ m.getMeetingType());
System.out.println("meetingDate: "+ m.getMeetingDate());
System.out.println("weatherConditio: "+ m.getWeatherCondition());
System.out.println("showCode->meetingCode: "+ m.getShowCode().getMeetingCode());
System.out.println("showCode->scheduledType: "+ m.getShowCode().getScheduledType());
System.out.println("venueType: "+ m.getVenueType());
for(ShowPools showPool : m.getShowPools()){
System.out.println("showPools->showProduct: "+ showPool.getShowProduct());
System.out.println("showPools->showStatus: "+ showPool.getShowStatus());
}
}
输出:
meetingName: GaryVon
location: USA
meetingType: P
meetingDate: 2016-03-25
weatherConditio: FINE
showCode->meetingCode: A
showCode->scheduledType: R
venueType: ANI
showPools->showProduct: GaryVon
showPools->showStatus: Open