我在java中创建和设置如下值
public Class creatObjectWithDefaultValue(String className) throws IllegalArgumentException, IllegalAccessException {
DefaultParamValues defaultParamValues = null;
Class objectClass = null;
try {
objectClass = Class.forName(className);
Field[] fields = objectClass.getClass().getDeclaredFields();
for(Field f:fields){
if(!f.isAccessible()){
f.setAccessible(true);
Class<?> type = f.getType();
if(type.equals(Integer.class)){
f.set(objectClass, defaultParamValues.INTEGER);
} else if(type.equals(BigInteger.class)){
f.set(objectClass, defaultParamValues.BIGINTEGER);
}else if(type.equals(LocalDate.class)){
f.set(objectClass, defaultParamValues.DATE);
}else if(type.equals(Boolean.class)){
f.set(objectClass, defaultParamValues.BOOLEAN);
}else if(type.equals(Long.class)){
f.set(objectClass, defaultParamValues.LONGVALUE);
}
f.setAccessible(false);
}
//System.out.println(f.get(objectClass));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return objectClass;
}
现在我想转换返回对象,因为JSON or JSON Array
我尝试了这样的事情,但它抛出异常The constructor JSONObject(Class) is undefined
System.out.println ( new JSONObject( te.creatObjectWithDefaultValue("com.hexgen.ro.request.CreateRequisitionRO") ).toString () );
请帮我纠正错误。
此致