Bụn*_* Bự -2 java android json gson
Json字符串:
[
//Object 1
{
TypeName:"CheckSpecificDday",
SpecificDay:"20160413",
Lunar:1
},
{
TypeName:"CheckSpecificDday",
SpecificDay:"20160414",
Lunar:1
},
//Object 2
{
TypeName:"CheckEveryDayDday",
StartDate:"20160413",
EndDate:"20260417",
Interval:1,
StartOption:"D",
HolidayCondition:1
},
//Object 3
{
TypeName:"CheckEveryDdayOfWeek",
StartDate:"20160413",
EndDate:"",
Interval:1,
SpecificDayOfWeek:"3",
HolidayCondition:1
},
//Object 4
{
TypeName:"CheckEveryMonthSpecificDday",
StartDate:"20160413",
EndDate:"",
Interval:1,
SpecificDD:"13,14",
HolidayCondition:1
},
//Object 5
{
TypeName:"CheckEveryYearWeek",
StartDate:"20160413",
EndDate:"",
Interval:1,
SpecificMMnthWeek:"0433",
HolidayCondition:1
}
]
Run Code Online (Sandbox Code Playgroud)
我有一个像上面这样的Json数组.我想要的是用Gson将它解析为不同的对象类型(正如我评论的那样让它更清晰),但我不知道该怎么做.请帮我.先感谢您!
var*_*ren 17
解析这个问题的一种方法是使用简单的方法
Object[] result = new Gson().fromJson(json, Object[].class);
Run Code Online (Sandbox Code Playgroud)
但这会给你对象LinkedTreeMap<Integer, LinkedTreeMap<String, String>>或类似的东西.你可以使用它,但它有点困难,你也会遇到整数作为双打的问题.
另一种方法是在TypeName需要时使用字段创建自定义接口或抽象类:
private interface CheckInterface{}
Run Code Online (Sandbox Code Playgroud)
并使用您拥有的每个POJO类对象类型实现它:
private static class CheckEveryDayBase implements CheckInterface{
private String StartDate;
private String EndDate;
private int Interval;
private int HolidayCondition;
}
private static class CheckSpecificDday implements CheckInterface{
private String SpecificDay;
private int Lunar;
}
private static class CheckEveryDayDday extends CheckEveryDayBase{
private String StartOption;
}
private static class CheckEveryDdayOfWeek extends CheckEveryDayBase{
private String SpecificDayOfWeek;
}
private static class CheckEveryMonthSpecificDday extends CheckEveryDayBase{
private String SpecificDD;
}
private static class CheckEveryYearWeek extends CheckEveryDayBase{
private String SpecificMMnthWeek;
}
Run Code Online (Sandbox Code Playgroud)
然后为您的CheckInterface:创建自定义解析器:
public static class CheckInterfaceDeserializer implements JsonDeserializer<CheckInterface>{
@Override
public CheckInterface deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
JsonObject jObject = (JsonObject) json;
JsonElement typeObj = jObject.get("TypeName");
if(typeObj!= null ){
String typeVal = typeObj.getAsString();
switch (typeVal){
case "CheckSpecificDday":
return context.deserialize(json, CheckSpecificDday.class);
case "CheckEveryDayDday":
return context.deserialize(json, CheckEveryDayDday.class);
case "CheckEveryDdayOfWeek":
return context.deserialize(json, CheckEveryDdayOfWeek.class);
case "CheckEveryMonthSpecificDday":
return context.deserialize(json, CheckEveryMonthSpecificDday.class);
case "CheckEveryYearWeek":
return context.deserialize(json, CheckEveryYearWeek.class);
}
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
以下是如何使用它:
GsonBuilder builder = new GsonBuilder();
// Register custom deserializer for CheckInterface.class
builder.registerTypeAdapter(CheckInterface.class, new CheckInterfaceDeserializer());
Gson gson = builder.create();
CheckInterface[] result2 = gson.fromJson(json, CheckInterface[].class);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4449 次 |
| 最近记录: |