title: How to save data with gson in a json file?
body: <p>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 used Gson, the problem is that just one record  saved in the file. Thanks.<br>
  Here the result in <code>file.json</code>:</p>
  
  <pre><code>{"data":
   [
   {"id":2,"Name":"Mike"}
   ]
  }
  </code></pre>
  
  <p><strong>servlet.java</strong></p>
  
  <pre><code>SqlSession session = MyBatisSqlSessionFactory.getSession();
  List&lt;User&gt; users = session.selectList("dao.UserDao.findAll");
  for (User u : users) {
      Gson gson = new Gson();
      try {
          JsonWriter  writer = new JsonWriter(new FileWriter("C:\\file.json"));
          writer.beginObject();
          writer.name("data");
          writer.beginArray();
          writer.beginObject();
          writer.name("id").value(t.getId());
          writer.name("name").value(t.getNom());
          writer.endObject();
          writer.endArray();
          writer.endObject();
          writer.close();
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  
  session.close();
  </code></pre>