我试图实施应该从表中检索数据和JSON格式返回结果Web服务的Java方法。为此,我使用json_agg()函数将查询结果转换为我想要显示的json,但到目前为止它显示的格式不正确。下面是该方法本身:正确的显示转换SQL结果集为JSON
public String GetRowsFromTable() throws SQLException {
Connection connection = null;
String result = "";
try {
connection = Connection.getConnection();
PreparedStatement prepStmt = conn.prepareStatement("SELECT json_agg(table1) FROM table");
ResultSet rs = prepStmt.executeQuery();
result += rs;
System.out.println(rs);
} catch (SQLException sqlex) {
sqlex.printStackTrace();
} finally {
connection.close();
}
return result;
}
控制台显示以下结果:
[email protected]
任何帮助,将不胜感激。
*,但它显示格式错误到目前为止*,这意味着绝对没有。 –
也就是的ToString()的'默认输出''上和Object'不正确的事情上'ResultSet'打电话,只看了'ResultSet'的源代码,它只是没有在'Object'默认。我认为你需要阅读关于你想要做什么的文档,或者如果你已经有了。 –