我需要一些帮助来将此JSON响应反序列化为可以进一步使用的数组
我得到的Json Array响应有两个实体tradingHours和shutDownHours
为Json Response创建一个Json阅读器,并在阅读器中循环,直到我到达shutDown节点,然后尝试使用ShutdownHours类获取详细信息
“我无法使用交易时间课程从JSON响应中创建gson对象,我在这里缺少什么
JSON响应
{
"licenceNumber": "123319",
"tradingHours": [
{
"effectiveStartDate": "1947-12-01T00:00:00",
"effectiveEndDate": "2008-06-30T00:00:00",
"details": [
{
"day": "Monday",
"startTime": "5:00 AM",
"endTime": "12:00 midnight"
},
{
"day": "Tuesday",
"startTime": "5:00 AM",
"endTime": "12:00 midnight"
},
{
"day": "Wednesday",
"startTime": "5:00 AM",
"endTime": "12:00 midnight"
},
{
"day": "Thursday",
"startTime": "5:00 AM",
"endTime": "12:00 midnight"
},
{
"day": "Friday",
"startTime": "5:00 AM",
"endTime": "12:00 midnight"
},
{
"day": "Saturday",
"startTime": "5:00 AM",
"endTime": "12:00 midnight"
},
{
"day": "Sunday",
"startTime": "10:00 AM",
"endTime": "11:00 PM"
},
{
"day": "Good Friday",
"startTime": "10:00 AM",
"endTime": "10:00 PM"
},
{
"day": "Christmas Day",
"startTime": "10:00 AM",
"endTime": "10:00 PM"
},
{
"day": "December 31st",
"startTime": "10:00 AM",
"endTime": "10:00 PM"
}
]
},
{
"effectiveStartDate": "2008-07-01T00:00:00",
"effectiveEndDate": null,
"details": [
{
"day": "Monday",
"startTime": "5:00 AM",
"endTime": "5:00 AM"
},
{
"day": "Tuesday",
"startTime": "5:00 AM",
"endTime": "5:00 AM"
},
{
"day": "Wednesday",
"startTime": "5:00 AM",
"endTime": "5:00 AM"
},
{
"day": "Thursday",
"startTime": "5:00 AM",
"endTime": "5:00 AM"
},
{
"day": "Friday",
"startTime": "5:00 AM",
"endTime": "5:00 AM"
},
{
"day": "Saturday",
"startTime": "5:00 AM",
"endTime": "5:00 AM"
},
{
"day": "Sunday",
"startTime": "5:00 AM",
"endTime": "5:00 AM"
},
{
"day": "Good Friday",
"startTime": "5:00 AM",
"endTime": "5:00 AM"
},
{
"day": "December 24th",
"startTime": "5:00 AM",
"endTime": "5:00 AM"
},
{
"day": "Christmas Day",
"startTime": "5:00 AM",
"endTime": "5:00 AM"
},
{
"day": "December 31st",
"startTime": "5:00 AM",
"endTime": "5:00 AM"
}
]
}
],
"shutdownHours": [
{
"effectiveStartDate": "2003-02-27T00:00:00",
"isPrimary": "Y",
"details": [
{
"day": "Monday",
"startTime": "2:00 AM",
"endTime": "8:00 AM"
},
{
"day": "Tuesday",
"startTime": "2:00 AM",
"endTime": "8:00 AM"
},
{
"day": "Wednesday",
"startTime": "2:00 AM",
"endTime": "8:00 AM"
},
{
"day": "Thursday",
"startTime": "2:00 AM",
"endTime": "8:00 AM"
},
{
"day": "Friday",
"startTime": "2:00 AM",
"endTime": "8:00 AM"
},
{
"day": "Saturday",
"startTime": "2:00 AM",
"endTime": "8:00 AM"
},
{
"day": "Sunday",
"startTime": "2:00 AM",
"endTime": "8:00 AM"
},
{
"day": "PublicHoliday",
"startTime": "4:00 AM",
"endTime": "10:00 AM"
}
]
},
{
"effectiveStartDate": "1947-12-01T00:00:00",
"isPrimary": null,
"details": [
{
"day": "Monday",
"startTime": "4:00 AM",
"endTime": "10:00 AM"
},
{
"day": "Tuesday",
"startTime": "4:00 AM",
"endTime": "10:00 AM"
},
{
"day": "Wednesday",
"startTime": "4:00 AM",
"endTime": "10:00 AM"
},
{
"day": "Thursday",
"startTime": "4:00 AM",
"endTime": "10:00 AM"
},
{
"day": "Friday",
"startTime": "4:00 AM",
"endTime": "10:00 AM"
},
{
"day": "Saturday",
"startTime": "4:00 AM",
"endTime": "10:00 AM"
},
{
"day": "Sunday",
"startTime": "4:00 AM",
"endTime": "10:00 AM"
},
{
"day": "PublicHoliday",
"startTime": "4:00 AM",
"endTime": "10:00 AM"
}
]
}
]
}
Java CODE
public static TradingHours readTradingHours(JsonReader reader) throws IOException {
ShutdownHours tradingHours = null;
reader.beginObject();
Gson gson = new Gson();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("tradingHours")) {
reader.beginArray();
tradingHours = gson.fromJson(reader, ShutdownHours.class);
}
else {
reader.skipValue();
}
}
return tradingHours;
}
TRADING HOURS CLASS , This class is used to create an object and is being used to create gson
package com.tattsgroup.site.entity.model;
import com.google.gson.annotations.SerializedName;
import java.io.Serializable;
import java.util.List;
public class TradingHours implements Serializable {
@SerializedName("effectiveStartDate")
private String effectiveStartDate;
@SerializedName("effectiveEndDate")
private String getEffectiveEndDate;
@SerializedName("details")
private List<TradingHourDetails> tradingHoursDetails;
public TradingHours() {
}
public TradingHours(String effectiveStartDate, String getEffectiveEndDate, List<TradingHourDetails> tradingHoursDetails) {
this.effectiveStartDate = effectiveStartDate;
this.getEffectiveEndDate = getEffectiveEndDate;
this.tradingHoursDetails = tradingHoursDetails;
}
public String getEffectiveStartDate() {
return effectiveStartDate;
}
public void setEffectiveStartDate(String effectiveStartDate) {
this.effectiveStartDate = effectiveStartDate;
}
public String getGetEffectiveEndDate() {
return getEffectiveEndDate;
}
public void setGetEffectiveEndDate(String getEffectiveEndDate) {
this.getEffectiveEndDate = getEffectiveEndDate;
}
public List<TradingHourDetails> getTradingHoursDetails() {
return tradingHoursDetails;
}
public void setTradingHoursDetails(List<TradingHourDetails> tradingHoursDetails) {
this.tradingHoursDetails = tradingHoursDetails;
}
}
答案 0 :(得分:1)
我假设您不必使用JsonReader
,只想从您提供的Json反序列化TradingHours
。我想这可能与JsonReader
有关,但有一种更简单的方法可以做到。
创建一个描述响应的类 - 它包含响应中的所有内容 - 实际上只是:
@Getter
public class HoursResponse {
private Long licenceNumber; // you can remove licenseNumber if not needed
private TradingHours[] tradingHours;
// add also this if needed later
// private ShutdownHours shutdownHours;
}
反序列化该类:
HoursResponse hr = gson.fromJson(json, HoursResponse.class);
并通过以下方式访问TradingHours
TradingHours[] tradingHours = hr.getTradingHours();
另外,我建议您将班级名称TradingHours
更改为TradingHour
,将TradingHourDetails
更改为TradingHourDetail
。复数通常用于引用列表等的名称。例如,以下更有意义:
TradingHour[] tradingHours = hr.getTradingHours();