It keeps printing "unsuccessful login" even though the json file it is reading from actually contains {"beansly":"beans"} on the first line and the only line.
Is there any way to have gson parse the key value pair and return the value of the key "beansly"?
in login()
private static void login(String username, String password) throws IOException {
JsonElement jsonData = readOrCreateNew(new File(".\\data.json")).get(username);
String passwordData = new Gson().fromJson(jsonData, String.class);
if (passwordData == password) {
System.out.println("Login successful");
}
else if (passwordData != password) {
System.out.println("Login unsuccessful");
}
}
in readOrCreateNew()
private static JsonObject readOrCreateNew(File f) throws IOException {
Gson gson = new Gson();
if (f.exists() && f.length() > 0) {
// Using "try" with parentheses to automatically close the BufferedReader
try (BufferedReader reader = new BufferedReader(new FileReader(f))) {
return gson.fromJson(reader, JsonObject.class);
}
}
return new JsonObject();
}