我正在面对这个Java.lang.nosuchFeildExecption 在执行代码时 谁能帮助我,如何解决这个问题? 提前致谢 我正在面对这个Java.lang.nosuchFeildExecption 在执行代码时..
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)
给定路径 例如:File file = new File(“ D:\ myJSONFile.txt”); 或根据您的要求给出路径 在Windows机器 字符串absoluteFilePath = fileSeparator +“用户” + fileSeparator +“ roopesh” + fileSeparator +“ myJSONFile.txt”; 文件文件=新文件(absoluteFilePath);
答案 1 :(得分:0)
可以将文件添加(假设可以)到 src / main / resources 文件夹中,然后使用java.nio包读取文件:
Path path = Path.get(getClass().getClassLoader().getResource("myJSONFile.txt").toURI());
byte[] fileBytes = Files.readAllBytes(path);
String theString = new String(fileBytes, "UTF-8");
现在要阅读课程和领域:
Class<?> c = Class.forName("<give class name with package>");
//whatever field is attempted to be read
Field f = c.getDeclaredField("fieldName");
该字段应该出现在班级中。
示例:
private String fieldName;