JAVA bug solution: put(JSONObject) is undefined for the type JSONArray

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

Intelligent Recommendation

JSONArray is assigned to JSONObject as a string format solution

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

JsonObject and JsonArray Simply put, the Json object is stored in the JsonArray....

jsonarray and jsonobject

        ...

JSONOBJECT / JSONARRAY

1, import dependence 2, write test class Console output:...

More Recommendation

jsonObject,jsonArray

JSONOBJECT, JSONARRAY Dependence` JsonObject is simple to use result ``{“name”:234,“id”:123} [{“name”:234,“id”:123}]...

JSONObject、JSONArray

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...

Java converts jsonarray to jsonobject corresponding to the key value

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

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...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top