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

无论是使用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个回答

一般情况下是没有这种问题的,但是如果使用java对象转化为json时,很有可能出现死循环。这中死循环在使用Hibernate和json-lib时是常出现的问题,可以通过setExclude()方法解决。

详细参看:http://jiangzhengjun.iteye.com/blog/467246

建议使用Jackson,Jackson提供了最好的性能,转换Java对象可以是List,POJO[],POJO,也可以是Map名值对。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问