我創建和設置值象Java以下爪哇 - 反思 - 轉換爲JSON陣列
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());
請幫我改正錯誤。
最好的問候
Understood Philipp我該如何將類轉換爲JSON對象或Json數組 – 2013-04-29 09:34:19