saeid ezzati
saeid ezzati

Reputation: 891

how convert database data to json string

I have a table in database with 7 columns and want to send the column #0 and #6 of all rows to my server as json.

type of column 0 is Long and 6 is string.

my code:

Cursor c = datasource.db.rawQuery("select * from posts ",null);
    if (c.moveToFirst()) {
                        ArrayList<Object> list = new ArrayList<Object>();
                        do{
                            Object[] b = new Object[]{c.getLong(0),c.getString(6)};
                            list.add(b);
                        } while  (c.moveToNext());
                        JSONArray jsArray = new JSONArray(list);

                        Log.d("states",jsArray.toString());
                    }

my desired output is this:

[[123,"hash1"],[125,"hash2"]]

but app gives me this output:

["[Ljava.lang.String;@b212d8b8","[Ljava.lang.String;@b212d998"]

Upvotes: 0

Views: 668

Answers (1)

rcorbellini
rcorbellini

Reputation: 1337

i think you need a array list of JSONArray with primitive type

ArrayList<JSONArray> list = new ArrayList<JSONArray>();

and add values this way

JSONArray jArray = new JSONArray(); 
jArray.put(c.getLong(0));
jArray.put(c.getLong(6));
list.add(jArray);

Upvotes: 2

Related Questions