[英]JSON parse database to String
我創建了一個對數據庫使用JSON的應用程序,看起來寫得很好,文件讀取器很好地讀取了數據庫,但是我似乎無法從數據庫值中獲取數據庫值。
這是我的解析代碼:
String userEnteredString = UserEntered.getText();
String userHomeLocal = Tutschedule.userHome;
Reader dataFile = null;
try {
dataFile = new FileReader(userHomeLocal+"/Users/"+userEnteredString+".data");
} catch (FileNotFoundException ex) {
Logger.getLogger(LoginForm.class.getName()).log(Level.SEVERE, null, ex);
}
String dbData = dataFile.toString();
try {
JSONObject dbObject = new JSONObject(dbData);
} catch (JSONException ex) {
Logger.getLogger(LoginForm.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(dbData);
JSONObject dataInfo = new JSONObject(dbData);
String password = dataInfo.getString("password");
System.out.println(password);
BufferedReader buffered = new BufferedReader(dataFile);
String test = null;
try {
test = buffered.readLine();
} catch (IOException ex) {
Logger.getLogger(LoginForm.class.getName()).log(Level.SEVERE, null, ex);
}
問題是,當我打印密碼時,它什么也不會打印,導致我認為未處理密碼字段。
這是數據庫的示例:
{"username":"user","password":"test"}
謝謝!
我不相信FileReader.toString()正在做您認為正在做的事情。 FileReader從object繼承toString,這意味着它只是打印出引用,而不是文件的內容,而您正在嘗試將其解析為JSON。 在那種情況下,您應該有一條嚴重的日志消息。
為了讀取文件的內容,您應該在read
器上使用read
方法,或者使自己更輕松,並使用commons-io FileUtils#readFileToString或類似的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.