【问题标题】:android Cursor to JSONArrayandroid 光标到 JSONArray
【发布时间】:2017-12-25 17:46:24
【问题描述】:

如何将光标“转换”为 JSONArray?

我的光标为 3 列(_id、姓名、出生)

我已经搜索过,但找不到任何示例

【问题讨论】:

    标签: android json android-cursor


    【解决方案1】:

    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;
    
    }
    

    【讨论】:

    • 这里是什么totalColumn?
    • int totalColumn = cursor.getColumnCount();
    • 谢谢。我已经想通了。这段代码对我来说非常好。
    • 简化cursor.isAfterLast() == false => !cursor.isAfterLast()
    • 太棒了!谢谢!
    【解决方案2】:
    private String cursorToString(Cursor crs) {
        JSONArray arr = new JSONArray();
        crs.moveToFirst();
        while (!crs.isAfterLast()) {
            int nColumns = crs.getColumnCount();
            JSONObject row = new JSONObject();
            for (int i = 0 ; i < nColumns ; i++) {
                String colName = crs.getColumnName(i);
                if (colName != null) {
                    String val = "";
                    try {
                        switch (crs.getType(i)) {
                        case Cursor.FIELD_TYPE_BLOB   : row.put(colName, crs.getBlob(i).toString()); break;
                        case Cursor.FIELD_TYPE_FLOAT  : row.put(colName, crs.getDouble(i))         ; break;
                        case Cursor.FIELD_TYPE_INTEGER: row.put(colName, crs.getLong(i))           ; break;
                        case Cursor.FIELD_TYPE_NULL   : row.put(colName, null)                     ; break;
                        case Cursor.FIELD_TYPE_STRING : row.put(colName, crs.getString(i))         ; break;
                        }
                    } catch (JSONException e) {
                    }
                }
            }
            arr.put(row);
            if (!crs.moveToNext())
                break;
        }
        crs.close(); // close the cursor
        return arr.toString();
    }
    

    【讨论】:

    • 这绝对是我需要的,因为我的Cursor有不同类型的数据。
    【解决方案3】:

    您不能将游标的内容直接转换为 JSONObject,但您可以通过一些逻辑来做到这一点。

    例如:从光标中检索字符串,形成一个遵循 JSON 格式的字符串,并用它来制作一个 json 对象:

    JSONObject jFromCursor=new JSONObject(string_in_JSON_format);
    

    【讨论】:

    • 所以我需要迭代游标并连接字符串 ("_id: " + cursor.getString(0) ", "name: "" + cursor.getString(1) ", "birth: "" + cursor.getString(2))???
    • 如果获取 JSON 对象的全部原因是然后使用 toString() 将其转换为 JSON 格式的字符串,这只是愚蠢的!
    猜你喜欢
    • 1970-01-01
    • 2015-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 2015-07-21
    • 1970-01-01
    相关资源
    最近更新 更多