How to save data with gson in a json file?

You write all the users in same file C:\\file.json so just the last iteration of the loop saved.

You can convert the object List<User> into json and write it once (no needed loop)

Example:

try (Writer writer = new FileWriter("Output.json")) {
    Gson gson = new GsonBuilder().create();
    gson.toJson(users, writer);
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)