我正在尝试设置一个 Web 应用程序以通过 JSON 发送数据库表。我想毫不费力地发送所有参赛作品。所以我将所有字段都以ArrayList的形式读出,现在我可以通过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;
}
最佳答案
您可以尝试在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;
}
关于java - 有没有一种方便的方法将多个ArrayList打包到JsonArray中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32758901/