title: ResultSet to Json in java servlet
body: <p>Trying to get data from database and storing in JSON object as below:</p>
  
  <pre><code>try {
              JsonObject jsonResponse = new JsonObject();
              JsonArray data = new JsonArray();
              JsonArray row = new JsonArray();
              PrintWriter out = response.getWriter();
  
              Connection con = OracleDBConnection.getConnection();
              String query = "Select * from CUSTOMER_DEMOGRAPHICS";
              Statement st = con.createStatement();
              ResultSet rSet = st.executeQuery(query);
  
              while (rSet.next()) 
              {
                  row.add(new JsonPrimitive(rSet.getString("COUNTRY")));
                  row.add(new JsonPrimitive(rSet.getString("STATE")));
                  row.add(new JsonPrimitive(rSet.getString("CITY")));
                  data.add(row);
              }
  
              jsonResponse.add("ResponseData", data);
              response.setContentType("application/json");
              response.setCharacterEncoding("UTF-8");
  
              out.print(jsonResponse);
              out.flush();
              System.out.println(jsonResponse);
  
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>Table structure in database:</p>
  
  <pre><code>COUNTRY     STATE       CITY
  ================================
  AUSTRALIA   VICTORIA    MELBOURNE
  AUSTRALIA   NEW SOUTH   SYDNEY
  </code></pre>
  
  <p>The problem is when I print the json, it prints twice for each row as follows rather than once.</p>
  
  <p><code>{
  "ResponseData":</code>
  <code>[["AUSTRALIA","VICTORIA","MELBOURNE","AUSTRALIA ","NEW SOUTH WALES","SYDNEY"],</code>
  <code>["AUSTRALIA","VICTORIA","MELBOURNE","AUSTRALIA ","NEW SOUTH WALES","SYDNEY"]]
  }</code></p>