android Cursor to JSONArray
Asked Answered
B

3

11

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

Brenna answered 25/10, 2012 at 14:25 Comment(0)
B
5

You can't convert the contents of a cursor directly into a JSONObject, but you can do that with some logic.

for eg: retrieve the Strings from the cursor, form a String which follows the JSON format, and use it to make a json object :

JSONObject jFromCursor=new JSONObject(string_in_JSON_format);
Bridges answered 25/10, 2012 at 14:32 Comment(2)
So I need to iterate the cursor and concatenate the Strings ("_id: " + cursor.getString(0) ", "name: "" + cursor.getString(1) ", "birth: "" + cursor.getString(2))???Brenna
Well this is just silly if the whole reason to get a JSON object is to then convert it to a string in JSON format using toString()!Springwood
L
25

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;

}
Lanlana answered 28/9, 2014 at 11:42 Comment(5)
What it totalColumn here?Rotund
int totalColumn = cursor.getColumnCount();Lanlana
Thanks. I had figured this out. This code works perfectly fine for me.Rotund
Simplify cursor.isAfterLast() == false => !cursor.isAfterLast()Quantify
Brilliant! Thank you!Fibrosis
P
12
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();
}
Prostrate answered 30/1, 2014 at 10:55 Comment(1)
It's absolutely what I need, because my Cursor have different types of data.Theodolite
B
5

You can't convert the contents of a cursor directly into a JSONObject, but you can do that with some logic.

for eg: retrieve the Strings from the cursor, form a String which follows the JSON format, and use it to make a json object :

JSONObject jFromCursor=new JSONObject(string_in_JSON_format);
Bridges answered 25/10, 2012 at 14:32 Comment(2)
So I need to iterate the cursor and concatenate the Strings ("_id: " + cursor.getString(0) ", "name: "" + cursor.getString(1) ", "birth: "" + cursor.getString(2))???Brenna
Well this is just silly if the whole reason to get a JSON object is to then convert it to a string in JSON format using toString()!Springwood

© 2022 - 2024 — McMap. All rights reserved.