JAVA BUG解決: put(JSONObject) is undefined for the type JSONArray

博主在從網上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
在這裏插入圖片描述

發佈了20 篇原創文章 · 獲贊 26 · 訪問量 1795
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章