0

I have project with a JSON file "userData.json".

The file is in the same directory as my java files.

I moved the file directly under the project root folder and it did not fix the problem:

Project_Root
    src
    userData.json 

Code of the java file that attempts to open userData.json:

private boolean checkData(String username, String password) throws FileNotFoundException {
    //read userData.json
    JSONObject jsonObject = new JSONObject(new JSONTokener(new FileReader("group18/src/main/java/com/example/group18/userData.json")));
    JSONArray jsonArray = jsonObject.getJSONArray("users1");

    //loop through the json array and compare the username and password
    if(jsonArray.getJSONObject(0).getString("username").equals(username) && jsonArray.getJSONObject(0).getString("password").equals(password)){
        jsonArray.getJSONObject(0).put("online", true);
        //return true if the username and password are correct
        return true;
    }
        return false;
}
Lmabear
  • 1
  • 2

1 Answers1

0

For reading userData.json with the above project structure

JSONObject jsonObject = new JSONObject(new JSONTokener(new FileReader("userData.json")));
krishnan
  • 132
  • 5