【问题标题】:Is there a convenient way to pack multiple ArrayLists into a JsonArray?有没有一种方便的方法可以将多个 ArrayLists 打包到一个 JsonArray 中?
【发布时间】:2015-09-24 10:21:48
【问题描述】:

我正在尝试设置一个 web 应用程序以通过 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;
}

【问题讨论】:

  • 不使用 JSONArray[5],只需使用 JSONArray 及其元素即可。然后返回一个 JSONArray,其中包含每个数组作为元素。
  • @KDM 我不太确定我是否正确:如何将ArrayList 作为元素添加到JSONArray?有没有办法将多个 JSONArrays 添加到单个 JSONArray(嵌套)?
  • JSONArray js = new JSONArray();js.add(new JSONArray(text)); 等应该可以工作。 JSONArray 可以包含所有原语以及任何其他 JSON 对象。
  • 谢谢你的解释,现在清楚了

标签: java json arraylist


【解决方案1】:

您可以尝试在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;
}

【讨论】:

  • 1 up 的可重用性,简单明了的解决方案
  • 只是 if 语句现在已经过时了,我编辑了它
  • 我只是想从 JSON 中解析回来,我认为它应该与 ArrayList&lt;AplPojo&gt; entries = gson.fromJson(jsonString, ArrayList&lt;AplPojo&gt;.class) 一起使用,但似乎我必须遍历每个 AplPojo 元素,或者是否存在我错过了什么?
  • ...已经找到了:ArrayList&lt;DBEntries&gt; entries = gson.fromJson(jsonString, new TypeToken&lt;ArrayList&lt;DBEntries&gt;&gt;() {}.getType()); :-)
猜你喜欢
  • 1970-01-01
  • 2022-06-11
  • 1970-01-01
  • 2011-08-24
  • 1970-01-01
  • 1970-01-01
  • 2020-01-21
  • 1970-01-01
相关资源
最近更新 更多