android Cursor to JSONArray

前端 未结 3 647
青春惊慌失措
青春惊慌失措 2021-02-14 21:52

how can I \"convert\" a Cursor to a JSONArray?

my cursor as 3columns (_id, name, birth)

I\'ve searched but I can\'t not find any examples

3条回答
  •  自闭症患者
    2021-02-14 22:41

    Cursor to JSONArray

    public JSONArray cur2Json(Cursor cursor) {
    
        JSONArray resultSet = new JSONArray();
        cursor.moveToFirst();
        while (cursor.isAfterLast() == false) {
            int totalColumn = cursor.getColumnCount();
            JSONObject rowObject = new JSONObject();   
            for (int i = 0; i < totalColumn; i++) {
                if (cursor.getColumnName(i) != null) {
                    try {
                        rowObject.put(cursor.getColumnName(i),
                                cursor.getString(i));
                    } catch (Exception e) {
                        Log.d(TAG, e.getMessage());
                    }
                }
            }
            resultSet.put(rowObject);
            cursor.moveToNext();
        }
    
        cursor.close();
        return resultSet;
    
    }
    

提交回复
热议问题