tags: eclipse java servlet json bug
The blogger has this bug in the code to convert ResultSet to JSON copied from the Internet
public static String resultSetToJson(ResultSet rs) throws SQLException,JSONException
{
// json array
JSONArray array = new JSONArray();
// Get the number of columns
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
// Traverse each piece of data in the ResultSet
while (rs.next()) {
JSONObject jsonObj = new JSONObject();
// loop through each column
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 error is reported in this line The method put(JSONObject) is undefined for the type JSONArray
}
return array.toString();
}
After searching a lot of information on the Internet, I found that I only need to change the error line to the following
array.add(jsonObj);
Reference from stackoverflow
https://stackoverflow.com/questions/26456791/putjsonobject-is-undefined-for-the-type-jsonarray

JSONArray is assigned to JSONObject to parse json. After json is parsed, it is found that JSONArray becomes a string: JSONArray looks like a string: Normal appearance: solve: ...
JsonObject and JsonArray Simply put, the Json object is stored in the JsonArray....
1, import dependence 2, write test class Console output:...
JSONOBJECT, JSONARRAY Dependence` JsonObject is simple to use result ``{“name”:234,“id”:123} [{“name”:234,“id”:123}]...
The JSON used before, is a key corresponding to a value, a super simple one-to-one relationship. Now useful JSON that can be nestled in layers, taking a data so cumbersome. In fact, just like the IF e...
Sometimes when we manipulate data, a lot of data is in jsonarry format. such as: Such a format is very typed in a tabular data type. But we have to take the name with id 1 so we have to change the var...
Insert JSONObject in the first place in JSONArray in java I found a lot of methods on the Internet that didn't find every JSONObject data inserted in the first place of JSONArray. then, Write one by y...