REST API的响应始终返回具有以下结构的JSON:
{
"status": "<status_code>",
"data": <data_object>
}
我的问题是data
的值没有唯一类型,但它可以是String,JSON对象或JSON数组,具体取决于被调用的端点。我无法弄清楚如何以正确的方式反序列化它来创建不同的Java对象......
例如,我已经准备了一些POJO:根元素
public class ApiResult {
@SerializedName("status")
public String status;
@SerializedName("data")
public JsonElement data; // should I define it as a JsonElement??
}
和两个反映两个端点的对象:
// "data" can be a list of NavItems
public class NavItem {
@SerializedName("id")
public String id;
@SerializedName("name")
public String name;
@SerializedName("icon")
public String icon;
@SuppressWarnings("serial")
public static class List extends ArrayList<NavItem> {}
}
和
// "data" can be a single object representing a Profile
public class Profile {
@SerializedName("id")
public String id;
@SerializedName("fullname")
public String fullname;
@SerializedName("avatar")
public String avatar;
}
阅读一些StackOverflow问题,我看到我应该使用JsonDeserializer<T>
接口。但是data
中ApiResult
的类型是如何变量的呢?
答案 0 :(得分:4)
您应该使用自定义JsonDeserializer
并在那里写下您的所有逻辑,例如
<强> ApiResult.java 强>
public class ApiResult {
@SerializedName("status")
public String status;
@SerializedName("data")
public Object data;
}
<强> ApiResultDeserializer.java 强>
import java.lang.reflect.Type;
import java.util.List;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
public class ApiResultDeserializer implements JsonDeserializer<ApiResult> {
private Type listType = new TypeToken<List<NavItem>>(){}.getType();
@Override
public ApiResult deserialize(JsonElement value, Type type,
JsonDeserializationContext context) throws JsonParseException {
final JsonObject apiResultJson = value.getAsJsonObject();
final ApiResult result = new ApiResult();
result.status = apiResultJson.get("status").getAsString();
JsonElement dataJson = apiResultJson.get("data");
if(dataJson.isJsonObject()) {
result.data = context.deserialize(dataJson, NavItem.class);
} else if(dataJson.isJsonPrimitive()) {
result.data = context.deserialize(dataJson, String.class);
} else if(dataJson.isJsonArray()) {
result.data = context.deserialize(dataJson, listType);
}
return result;
}
}
并尝试按照您的提及创建不同类型的data
(List
,Object
或String
)
<强> Main.java 强>
Gson gson = new GsonBuilder()
.registerTypeAdapter(ApiResult.class, new ApiResultDeserializer())
.create();
List<NavItem> navItems = new ArrayList<NavItem>();
for(int i = 1 ; i < 6 ; ++i) {
navItems.add(new NavItem(i+"", "Name-" + i, "Icon-" + i ));
}
ApiResult result = new ApiResult();
result.status = "OK";
result.data = navItems;
// Serialization
System.out.println(gson.toJson(result)); // {\"status\":\"OK\",\"data\":[{\"id\":\"1\",\"name\":\"Name-1\",\"icon\":\"Icon-1\"},{\"id\":\"2\",\"name\":\"Name-2\",\"icon\":\"Icon-2\"},{\"id\":\"3\",\"name\":\"Name-3\",\"icon\":\"Icon-3\"},{\"id\":\"4\",\"name\":\"Name-4\",\"icon\":\"Icon-4\"},{\"id\":\"5\",\"name\":\"Name-5\",\"icon\":\"Icon-5\"}]}
result.data = navItems.get(0);
System.out.println(gson.toJson(result)); // {\"status\":\"OK\",\"data\":{\"id\":\"1\",\"name\":\"Name-1\",\"icon\":\"Icon-1\"}}
result.data = "Test";
System.out.println(gson.toJson(result)); // {\"status\":\"OK\",\"data\":\"Test\"}
// Deserialization
String input = "{\"status\":\"OK\",\"data\":[{\"id\":\"1\",\"name\":\"Name-1\",\"icon\":\"Icon-1\"},{\"id\":\"2\",\"name\":\"Name-2\",\"icon\":\"Icon-2\"},{\"id\":\"3\",\"name\":\"Name-3\",\"icon\":\"Icon-3\"},{\"id\":\"4\",\"name\":\"Name-4\",\"icon\":\"Icon-4\"},{\"id\":\"5\",\"name\":\"Name-5\",\"icon\":\"Icon-5\"}]}";
ApiResult newResult = gson.fromJson(input, ApiResult.class);
System.out.println(newResult.data); // Array
input = "{\"status\":\"OK\",\"data\":{\"id\":\"1\",\"name\":\"Name-1\",\"icon\":\"Icon-1\"}}";
newResult = gson.fromJson(input, ApiResult.class);
System.out.println(newResult.data); // Object
input = "{\"status\":\"OK\",\"data\":\"Test\"}";
newResult = gson.fromJson(input, ApiResult.class);
System.out.println(newResult.data); // String
答案 1 :(得分:0)
我设法让它按照我的意愿工作,而不使用任何自定义反序列化器!
对于每个端点,我等待响应(顺便说一句,我正在使用Volley),然后我首先生成“root”ApiResult对象,检查状态是否正常,然后我继续实例化数据字段作为请求的类型。
POJO与问题相同。在ApiResult
中,“数据”是JsonElement
。
// ... called the endpoint that returns a NavItem list
public void onResponse(String response) {
ApiResult rootResult = gson.fromJson(response.toString(), ApiResult.class);
if (rootResult.status.equals(STATUS_OK)) {
Log.d(LOG_TAG, response.toString());
NavItem.List resData = gson.fromJson(rootResult.data, NavItem.List.class); // <-- !!!!!
callback.onSuccess(resData);
}
else {
Log.e(LOG_TAG, response.toString());
callback.onError(-1, null);
}
}
显然,“Profile”端点唯一要改变的是!!!!!