博主在从网上copy的将ResultSet转化为JSON的代码中出现了这个BUG
public static String resultSetToJson(ResultSet rs) throws SQLException,JSONException
{
// json数组
JSONArray array = new JSONArray();
// 获取列数
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
// 遍历ResultSet中的每条数据
while (rs.next()) {
JSONObject jsonObj = new JSONObject();
// 遍历每一列
for (int i = 1; i <= columnCount; i++) {
String columnName =metaData.getColumnLabel(i);
String value = rs.getString(columnName);
jsonObj.put(columnName, value);
}
array.put(jsonObj); //报错在这一行 The method put(JSONObject) is undefined for the type JSONArray
}
return array.toString();
}
在网上找了很多资料后,发现仅仅需要将错误行改成如下即可
array.add(jsonObj);
参考自stackoverflow
https://stackoverflow.com/questions/26456791/putjsonobject-is-undefined-for-the-type-jsonarray