I'm trying to format my sqlite data from all my columns into a JSONArray.
My sqlite data example is as follows:
id | name | product |
1 | stev |[{"id":"1","title":"box"}]
1 | Lian |[{"id":"2","title":"bag"}]
1 | beny |[{"id":"3","title":"pen"}]
I am saving a JSONArray as a string in my database under the column product.
I am trying to create a jsonArray using gson library.
This is what i have tried:
public String composeJSONfromSQLite() {
ArrayList<HashMap<String, String>> offlineList;
offlineList = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT * FROM manyofflineCheckouts ";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put( cursor.getString(0));
map.put( cursor.getString(1));
map.put( cursor.getString(2));
offlineList.add(map);
} while (cursor.moveToNext());
}
database.close();
Gson gson = new GsonBuilder().create();
//Use GSON to serialize Array List to JSON
return gson.toJson(offlineList);
}
This however gives me in the format of:
[
{
"id":"1",
"name":"stev",
"product":"[{"id":"1","title":"box"}]"
},
{
"id":"2",
"name":"Lian",
"product":"[{"id":"2","title":"bag"}]"
}
]
This is not the correct Json format because of the double quotes surrounding product array "product":"[{"id":"1","title":"box"}]"
Is there a way to do this?