我正在尝试在运行时将JSON String转换为Java对象。可能吗?
String className = errorInfo.getClassName();
String methodName = errorInfo.getMethodName();
String requestMessage = errorInfo.getMessage();
String reference3 = errorInfo.getReference3();
try {
Class claz = Class.forName(className);
Object obj = claz.newInstance();
Class[] parameterTypes = new Class[1];
parameterTypes[0] = Class.forName(reference3).getClass();
Method method = claz.getMethod(methodName, parameterTypes);
method.invoke(obj, new
ObjectMapper().readValue(requestMessage,<I have to pass here .class reference>>));
}
catch(Exception ex)
{
}
我犯了基本错误。以下代码对我有用:
String className = errorInfo.getClassName();
String methodName = errorInfo.getMethodName();
String requestMessage = errorInfo.getMessage();
String reference3 = errorInfo.getReference3();
try {
Class<?> claz = Class.forName(className);
Object obj = claz.newInstance();
Class[] parameterTypes = new Class[1];
Class<?> parameter = Class.forName(reference3);
parameterTypes[0] = parameter;
Method method = claz.getMethod(methodName, parameterTypes);
method.invoke(obj, new
ObjectMapper().readValue(requestMessage,parameter));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}