gpt4 book ai didi

java - 结果集转换为 JSON。如何打印 json 对象?

转载 作者:行者123 更新时间:2023-11-30 06:31:26 24 4
gpt4 key购买 nike

我指的是这个 link但我稍微修改了我的方法..

这是我的代码:

 public JSONArray generateJSON(ResultSet rs) {

JSONArray respJSON = new JSONArray();

try {
java.sql.ResultSetMetaData rsmd = rs.getMetaData();
int numColumns = rsmd.getColumnCount();
while (rs.next()) {
JSONObject obj = new JSONObject();
for (int i = 1; i < numColumns + 1; i++) {

String columnName = rsmd.getColumnName(i);
if (rsmd.getColumnType(i) == java.sql.Types.ARRAY) {
obj.put(columnName, rs.getArray(i));
} else if (rsmd.getColumnType(i) == java.sql.Types.BIGINT) {
obj.put(columnName, rs.getInt(i));
} else if (rsmd.getColumnType(i) == java.sql.Types.BOOLEAN) {
obj.put(columnName, rs.getBoolean(i));
} else if (rsmd.getColumnType(i) == java.sql.Types.BLOB) {
obj.put(columnName, rs.getBlob(i));
} else if (rsmd.getColumnType(i) == java.sql.Types.DOUBLE) {
obj.put(columnName, rs.getDouble(i));
} else if (rsmd.getColumnType(i) == java.sql.Types.FLOAT) {
obj.put(columnName, rs.getFloat(i));
} else if (rsmd.getColumnType(i) == java.sql.Types.INTEGER) {
obj.put(columnName, rs.getInt(i));
} else if (rsmd.getColumnType(i) == java.sql.Types.NVARCHAR) {
obj.put(columnName, rs.getNString(i));
} else if (rsmd.getColumnType(i) == java.sql.Types.VARCHAR) {
obj.put(columnName, rs.getString(i));
} else if (rsmd.getColumnType(i) == java.sql.Types.TINYINT) {
obj.put(columnName, rs.getInt(i));
} else if (rsmd.getColumnType(i) == java.sql.Types.SMALLINT) {
obj.put(columnName, rs.getInt(i));
} else if (rsmd.getColumnType(i) == java.sql.Types.DATE) {
obj.put(columnName, rs.getDate(i));
} else if (rsmd.getColumnType(i) == java.sql.Types.TIMESTAMP) {
obj.put(columnName, rs.getTimestamp(i));
} else {
obj.put(columnName, rs.getObject(i));
}

}
respJSON.put(obj);
//respJSON.add(obj);

}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

respJSON.toString();

return respJSON;

System.out.print(respJSON.toString());
}

我的问题是我无法打印 json 字符串以在控制台中看到它...

我试过 respJSON.toString();而且它似乎不起作用..

你能帮帮我吗? ...

谢谢

最佳答案

这是一个简单的错字。而不是这个:

    respJSON.toString();
return respJSON;
System.out.print(respJSON.toString());

这样做

    System.out.print(respJSON.toString());
return respJSON;

关于java - 结果集转换为 JSON。如何打印 json 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9686732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com