How to save data with gson in a json file?

后端 未结 3 2074
野的像风
野的像风 2021-02-05 02:45

In my web application I succeed in displaying data in html table using mybatis. Now I want to save the records of the Mysql table in a json file and create an array of users, I

3条回答
  •  时光取名叫无心
    2021-02-05 03:47

    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 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);
    }
    

提交回复
热议问题