博主在從網上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