我的最后一个解决方案 - 定制
JsonDeserializer
json消息:
1) 成功消息
{"data": ["success"],
"message": "message_text"
}
2) 错误信息
{"message": {
"name": ["message_text"],
"email": ["message_text"],
"subject": ["message_text"],
"body": ["message_text"]
}}
也许它可以帮助某人。
public static class GeneralResponceDeserializer implements JsonDeserializer {
@Override
public Object deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
JsonObject jobject = (JsonObject) jsonElement;
Gson googleJson = new Gson();
ArrayList<String> dataList = null;
if (jobject != null && jobject.get("data") != null) {
JsonArray dataArray = jobject.get("data").getAsJsonArray();
dataList = googleJson.fromJson(dataArray, new TypeToken<List<String>>(){}.getType());
}
String message = null;
Message messageObject = null;
if (jobject != null && jobject.get("message") != null) {
try {
message = jobject.get("message").getAsString();
} catch (UnsupportedOperationException e) {
JsonElement messageObjectJson = (JsonObject) jobject.get("message").getAsJsonObject();
JsonObject jMessageobject = (JsonObject) messageObjectJson;
if (jMessageobject != null) {
JsonArray name = jMessageobject.getAsJsonArray("name");
JsonArray subject = jMessageobject.getAsJsonArray("subject");
JsonArray body = jMessageobject.getAsJsonArray("body");
JsonArray email = jMessageobject.getAsJsonArray("email");
ArrayList<String> nameDataList = null;
ArrayList<String> subjectDataList = null;
ArrayList<String> bodyDataList = null;
ArrayList<String> emailDataList = null;
if (name != null) {
nameDataList = googleJson.fromJson(name, new TypeToken<List<String>>(){}.getType());
}
if (subject != null) {
subjectDataList = googleJson.fromJson(subject, new TypeToken<List<String>>(){}.getType());
}
if (body != null) {
bodyDataList = googleJson.fromJson(body, new TypeToken<List<String>>(){}.getType());
}
if (email != null) {
emailDataList = googleJson.fromJson(email, new TypeToken<List<String>>(){}.getType());
}
messageObject = new Message(nameDataList, subjectDataList, bodyDataList, emailDataList);
}
}
}
return new GeneralResponceObject(dataList, message, messageObject);
}
}