有没有方便的方法将多个ArrayLists打包成JsonArray?

时间:2015-09-24 10:21:49

标签: java json arraylist

我正在尝试设置一个webapp来通过JSON发送数据库表。我想不经常发送所有条目。所以我把所有字段都读出来作为ArrayLists,现在我可以通过JSON逐个解析它们并发送它们。但是没有一种方便的方法将它们全部打包成一个JsonArray吗?

这是我的代码示例:

public static JSONArray[] getDBEntries(String tablename) {
    JSONArray[] js = new JSONArray[5];
    Connection conn = null;
    Statement st = null;
    ResultSet rs = null;
    ArrayList<String> text = null;
    ArrayList<String> altname = null;
    ArrayList<String> altname2 = null;
    ArrayList<String> icd10 = null;
    ArrayList<String> alphaid = null;
    int index = 0;
    try {
        InitialContext ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/users");
        conn = ds.getConnection();
        st = conn.createStatement();
        rs = st.executeQuery("SELECT * FROM " + tablename);
        while (rs.next()) {
            text.add(rs.getString("text"));
            if (altname != null) {
                altname.add(rs.getString("altname"));
            }
            if (altname2 != null) {
                altname2.add(rs.getString("altname2"));
            }
            if (icd10 != null) {
                icd10.add(rs.getString("icd10"));
            }
            if (alphaid != null) {
                alphaid.add(rs.getString("alphaid"));
            }
            index++;
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (rs != null) rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (st != null) st.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (conn != null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    js[0] = new JSONArray(text);
    js[1] = new JSONArray(altname);
    js[2] = new JSONArray(altname2);
    js[3] = new JSONArray(icd10);
    js[4] = new JSONArray(alphaid);
    return js;
}

1 个答案:

答案 0 :(得分:2)

您可以尝试在POJO中添加表数据。然后你可以使用Gson将其转换为JsonArray

 class AplPojo {
    private String text;
    private String altname;
    private String altname;
    private String icd10;
    private String alphaid;

     //getter and setters
  }
  public static String getDBEntries(String tablename) {

    Connection conn = null;
    Statement st = null;
    ResultSet rs = null;
    ArrayList<AplPojo> aplPojos = new ArrayList<AplPojo>();
    int index = 0;
    try {
        InitialContext ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/users");
        conn = ds.getConnection();
        st = conn.createStatement();
        rs = st.executeQuery("SELECT * FROM " + tablename);
        while (rs.next()) {
            AplPojo  aplPojo= new AplPojo();
            aplPojo.setText(rs.getString("text"));
            aplPojo.setAltname(rs.getString("altname"));
            aplPojo.setAltname2(rs.getString("altname2")); 
            aplPojo.setIcd10(rs.getString("icd10")); 
            aplPojo.setAlphaid(rs.getString("alphaid"));
            index++;
            aplPojos.add(aplPojo);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (rs != null) rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (st != null) st.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (conn != null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    } 
     // create a new Gson instance
     Gson gson = new Gson();
     // convert your list to jsonArray
     String aplPojosList = gson.toJson(aplPojos);
     return aplPojos;
}