此类来自供应商库:
public class JsonParser {
public <T> T parse(String json, Class<T> type) { ... }
}
这些是我的模型:
public class Video {
@Key
private String title;
public String getTitle() {
return title;
}
}
public class Response<TResult> {
@Key
private TResult result;
public TResult getResult() {
return result;
}
// ...
}
此代码有效:
JsonParser parser = new JsonParser();
String json = "{ \"title\": \"Hello world\" }";
Video video = parser.parse(json, Video.class);
此代码不起作用:(语法错误Response<Video>.class
)
JsonParser parser = new JsonParser();
String json = "{ \"result\" : { \"title\": \"Hello world\" } }";
Response<Video> videoResponse = parser.parse(reader, Response<Video>.class);
此代码有效:
public class VideoResponse extends Response<Video> {
}
...
JsonParser parser = new JsonParser();
String json = "{ \"result\" : { \"title\": \"Hello world\" } }";
Response<Video> videoResponse = parser.parse(reader, VideoResponse.class);
我的问题是:如何将Response<Video>
类parse
作为参数传递给方法而VideoResponse
不像那样创建。(在我的程序中,有很多类似的模型Video
,我不想复制我的代码来创建空类VideoResponse
,,,,UserResponse
等CommentResponse
)ActivityResponse