我有一些Json数据,我需要将每个值作为字符串或数字或返回的数据类型返回。 但是我面临着java.lang.ClassNotFoundException:验证 在java.net.URLClassLoader.findClass(未知来源)
public class Validation {
public void readJSON() throws Exception {
File file = new File("myJSONFile.txt");
String content = FileUtils.readFileToString(file, "utf-8");
// Convert JSON string to JSONObject
JSONObject tomJsonObject = new JSONObject(content);
System.out.println(tomJsonObject);
System.out.println(tomJsonObject.getString("age"));
validateByType(tomJsonObject, "age", null);
}
public void validateByType(JSONObject jsonString, String pathString, String typeString)
throws JSONException, ClassNotFoundException, NoSuchFieldException, SecurityException {
String jsonField = jsonString.getString(pathString);
// pathString = "age";
// typeString ="number";
Class<?> c = Class.forName("Validation");
Field f = c.getField(jsonField);
System.out.format("Type: %s%n", f.getType());
System.out.format("GenericType: %s%n", f.getGenericType());
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Test");
Validation v = new Validation();
try {
v.readJSON();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
在传递类的简单名称导致ClassNotFoundException时,Class.forName()方法将类的完全限定名称作为参数。