0

i want to make sqlite data to jsonarray but i get some problem i have write this code to parse from sqlite to arraylist with hasmap and change it to json with it use gson to convert

public ArrayList<HashMap<String, String>> tampil_semua_pesanan() {
    SQLiteDatabase database = this.getWritableDatabase();

    // deklarasikan sebuah arraylist yang bisa menampung hashmap
    ArrayList<HashMap<String, String>> arrayListBiodata = new ArrayList<HashMap<String, String>>();

    Cursor cursor = database.rawQuery("SELECT * FROM "+ TABLE_PESAN , null);

    // kursor langsung diarkan ke posisi paling awal data pada tabel_biodata
    if (cursor.moveToFirst()) {
        do {
            // deklarasikan sebuah hashmap, yang bisa menamp
            HashMap<String, String> hashMapBiodata = new HashMap<String, String>();

            hashMapBiodata.put("id", cursor.getString(0));
            hashMapBiodata.put("nama", cursor.getString(1));
            hashMapBiodata.put("jumlah", cursor.getString(2));

            // masukkan hashMapBiodata ke dalam arrayListBiodata
            arrayListBiodata.add(hashMapBiodata);

        } while (cursor.moveToNext());
    }
    cursor.close();
    return arrayListBiodata;
}

and this is how to parse it

ArrayList<HashMap<String, String>> arrayListPesan = sqLiteHelper.tampil_semua_pesanan();
        userFunction.gson(arrayListPesan);
        Gson gson = new Gson();
        String jsonPesan = gson.toJson(arrayListPesan);

but i get it is like this

[{"id":"11","jjumlah":"12","nama":"Ayam Goreng"},{"id":"11","jjumlah":"12","nama":"Ayam Goreng"},{"id":"11","jjumlah":"12","nama":"Ayam Goreng"}]

i want to be like this

["makanan":{"id":"11","jumlah":"12","nama":"Ayam Goreng"},{"id":"11","jumlah":"12","nama":"Ayam Goreng"},{"id":"11","jumlah":"12","nama":"Ayam Goreng"}]

can anyone help me

  • This link may help you : http://stackoverflow.com/questions/26022133/how-can-i-delete-the-namevaluepairs-key-from-the-jsonobject – Darsh Patel Mar 28 '15 at 06:48

1 Answers1

0

try this code hope so it's Work...

    public ArrayList<HashMap<String, HashMap<String, String>>> tampil_meja() {
    SQLiteDatabase database = this.getWritableDatabase();
    ArrayList<HashMap<String, HashMap<String, String>>> arrayListMeja = new ArrayList<HashMap<String, HashMap<String, String>>>();

    Cursor cursor = database.rawQuery("SELECT * FROM "+ TABLE_MEJA , null);

    HashMap<String, HashMap<String, String>> hashMap = new HashMap<String, HashMap<String, String>>();
    if (cursor.moveToFirst()) {
        do {
            HashMap<String, String> hashMapBiodata = new HashMap<String, String>();
            hashMapBiodata.put("id", cursor.getString(0));
            hashMap.put("makanan", hashMapBiodata);
        } while (cursor.moveToNext());
    }
    cursor.close();

    arrayListMeja.add(hashMap);
    return arrayListMeja;
}
Ravi Makvana
  • 2,872
  • 2
  • 24
  • 38