ssqzh
ssqzh
2010-05-08 14:56

关于Java生产JSON,内存占用特别大的问题!

  • javascript

无论是使用JSON-Lib 或者 GSON库,内存占用特别的大,而且生成的结果也特别的大!自己手动生成的还可以的,我的代码如下:

[code="java"]public static String getJSONArray(String sql) {
Connection conn = null;
JsonArray rul = null;
try {
conn = DbToolkit.getConnection();
Statement stat = conn.createStatement();
boolean isrs = stat.execute(sql);
int h = 0;
do {
if (isrs) {
rul = new JsonArray();
ResultSet rs = stat.getResultSet();
ResultSetMetaData rsmd = rs.getMetaData();
while (rs.next()) {
System.out.println(h++);
//JSONObject jo = new JSONObject();
JsonObject jo = new JsonObject();
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
//jo.put(rsmd.getColumnName(i), rs.getString(i));
jo.addProperty(rsmd.getColumnName(i), rs.getString(i));
System.out.print(String.format("%s,%s", rsmd.getColumnName(i), rs.getString(i)));
//rul.put(jo);
rul.add(jo);
}
System.out.println();
}
break;
}
isrs = stat.getMoreResults();
} while (stat.getUpdateCount() != -1 || isrs == true);
conn.close();
} catch (Exception ex) {
Logger.getLogger(SQL2JSON.class.getName()).log(Level.SEVERE, null, ex);
}

return rul.toString();
}

    public static void PrintWriteJson(String sql,PrintWriter out){
            Connection conn = null;
            try {
                    conn = DbToolkit.getConnection();
                    Statement stat = conn.createStatement();
                    boolean isrs = stat.execute(sql);
                    int h = 0;
                    do {
                            if (isrs) {
                                    ResultSet rs = stat.getResultSet();
                                    ResultSetMetaData rsmd = rs.getMetaData();
                                    out.print("[");
                                    while (rs.next()) {
                                            if(h++ >0){
                                                    out.print(",");
                                            }
                                            out.print("{");
                                            for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                                                    if(i>1){
                                                            out.print(",");
                                                    }
                                                    out.print(String.format("%s:\"%s\"", rsmd.getColumnName(i), rs.getString(i)));
                                            }
                                            out.print("}");
                                    }
                                    out.print("]");
                                    break;
                            }
                            isrs = stat.getMoreResults();
                    } while (stat.getUpdateCount() != -1 || isrs == true);
                    conn.close();
            } catch (Exception ex) {
                    Logger.getLogger(SQL2JSON.class.getName()).log(Level.SEVERE, null, ex);
            }
    }[/code]
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐