当然,我对所有这些 Java 东西都很陌生,所以我有一个问题,我正在尝试反序列化在 WCF 服务上获得的响应,一切正常,但是,我正在尝试制作一个通用函数来做到这一点.
基本上我所做的是
public List<msg> GetService(String method){
List<msg> response = new ArrayList<msg>();
Type msgType = new TypeToken<List<msg>>(){}.getType();
//Obtaining result
response = uJSON.fromJson(serviceResponse, msgType);
//uJSON is an instance of Gson library, for deserializing it just needs
//the service response and a Class<T> or Type to reflect the obtained message
}
我想要做的是获得类型“msg”泛型,这意味着......
public <thing> void GetInstanceService(String method){
List<thing> response = new ArrayList<thing>();
Type rType2 = new TypeToken<List<thing>>(){}.getType(); //Got java.util.List<thing>
//And when I'm trying to deserialize I just obtain a List of object
//[java.lang.Object@5c7a987e, java.lang.Object@74b1a7a0]
type2 = uJSON.fromJson(new String(entity), rType2);
}
但我是这样打电话的。
comm.<msgType>GetInstanceService("listTestType");
所以,当我调用“GetInstanceService”时,“thing”是“msgType”类型,
List<thing>
并且响应不应该List<msgType>
是List <Object>
?
此外,当我试图通过“类型”参数显式传递类型时,它只会导致我这样的编译时间错误。
public void GetInstanceService(Type type){
List<type> type2 = new ArrayList<type>(); //Compilation time error
//Or
msgType oType = new msgType();
Class classType = oType.getClass();
List<classType> type3; //Compilation time error
}
那么,如果这些尝试都无效,我该如何设置反序列化的类型呢?