[英]Java JSON can't get database values
我是Java的新手,似乎無法弄清問題出在哪,我對此事進行了調查,盡管我遵循了它的教程,但仍無法使它工作,但它看到了該文件,因為它提供了不同的內容當我嘗試打開實際上不存在的文件時出錯,但無法從數據庫中獲取變量
我的代碼:
String userEnteredString = UserEntered.getText();
String userHomeLocal = Tutschedule.userHome;
FileReader 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();
System.out.println(dbData);
JSONObject dataInfo = (JSONObject)dbData.parse(dataFile);
這是我的進口貨:
import java.io.*;
import java.util.Iterator;
//import java.io.FileNotFoundException;
import org.json.*;
//import java.io.FileReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.parser.*;
這是寫入數據庫的部分,我確信問題不會出在這里,因為它寫的很好,因為我檢查了它創建的數據庫及其所在的數據庫(當我將用戶發送到登錄表單的行不存在想要暫時創建一個用戶):
public class Tutschedule {
// TODO Add the MySQL Database Support
public static String userHome;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws JSONException {
boolean loggedIn = false;
if (loggedIn != true) {
LoginForm.LoginForm();
}
userHome = System.getProperty("user.home")+"/TutSchedule";
System.out.print(userHome);
Scanner scan = new Scanner(System.in);
String username = scan.next();
String password = scan.next();
JSONObject user = new JSONObject();
user.put("username", username);
user.put("password", password);
boolean dirCreate;
String directories =userHome+"/Users";
dirCreate = (new File(directories)).mkdirs();
try {
FileWriter userDataFile = new FileWriter(userHome+"/Users/"+username+".data");
userDataFile.write(user.toString());
userDataFile.flush();
userDataFile.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.print(user);
}
}
我懷疑您可能需要更改此設置
JSONObject dataInfo = (JSONObject)dbData.parse(dataFile);
至
JSONObject dataInfo = (JSONObject)JSONValue.parse(dataFile);
因為dbData是一個String,並且沒有parse()方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.