-3

I want to convert arraylist to json object. I did find everything in stackoverflow but could not get my answer. Here is the format which i want.

$str = '{
"0": {
    "product_name 2": "Johnson Baby (Soap)",
    "product_total 1": "288"",
    "product_quantity": "3"
},
"1": {
    "product_name 2": "Johnson Baby (Soap)",
    "product_total 1": "288"",
    "product_quantity": "3"
}
}';

But i am unable to achieve this what i am getting is like this.

    {
    "product_qtn": {
        "Quantity 2": "4",
        "Quantity 1": "3",

    },
    "product_total": {
        "Price1": "288",
        "Price2": "112",

    },
    "product_name": {
        "Name3": "Johnson Baby (Soap)",
        "Name2": "Johnson baby (powder)",

    }
}

I am getting data from sqlite and storing in arraylist here is the code.

     dataBase = mHelper.getWritableDatabase();
     mCursor = dataBase.rawQuery("SELECT * FROM "
            + DBHelper.TABLE_NAME + " WHERE " + DBHelper.KEY_COUNT
            + " IS NOT NULL AND " + DBHelper.KEY_COUNT + " != '0'", null);
    product_price.clear();
    productprice = 0;
    if (mCursor.moveToFirst()) {
        do {


            product_price.add(mCursor.getString(mCursor
                    .getColumnIndex(DBHelper.KEY_PRODUCT_TOTAL_PRICE)));
            product_name.add(mCursor.getString(mCursor
                    .getColumnIndex(DBHelper.KEY_PNAME)));
            product_quantity.add(mCursor.getString(mCursor
                    .getColumnIndex(DBHelper.KEY_COUNT)));

        } while (mCursor.moveToNext());
    }
Nitesh
  • 318
  • 3
  • 16
  • I tried very hard to find where you are creating your JSON but `i really tried a lot but could not find it` – Scary Wombat May 12 '16 at 05:16
  • Need to reconstruct your ArraryList and then Try this http://stackoverflow.com/a/4842019/3981656 – Sathish Kumar J May 12 '16 at 05:16
  • 1
    Nothing you've shown is a JSON Array, because a JSON Array uses `[` and `]`. What you show in the first example is a JSON Object using numeric keys with values also being JSON Objects with 3 named keys, so it's basically a `Map`. – Andreas May 12 '16 at 05:20

3 Answers3

1

Try this

     dataBase = mHelper.getWritableDatabase();
 mCursor = dataBase.rawQuery("SELECT * FROM "
        + DBHelper.TABLE_NAME + " WHERE " + DBHelper.KEY_COUNT
        + " IS NOT NULL AND " + DBHelper.KEY_COUNT + " != '0'", null);
product_price.clear();
productprice = 0;

JSONObject Root = new JSONObject();
JSONArray productArray = new JSONArray();

if (mCursor.moveToFirst()) {
    do {
        JSONObject product = new JSONObject();
        /*
        product_price.add(mCursor.getString(mCursor
                .getColumnIndex(DBHelper.KEY_PRODUCT_TOTAL_PRICE)));
        product_name.add(mCursor.getString(mCursor
                .getColumnIndex(DBHelper.KEY_PNAME)));
        product_quantity.add(mCursor.getString(mCursor
                .getColumnIndex(DBHelper.KEY_COUNT)));*/

        product.put("product_name", mCursor.getString(mCursor
                .getColumnIndex(DBHelper.KEY_PRODUCT_TOTAL_PRICE)));

        product.put("product_total", mCursor.getString(mCursor
                .getColumnIndex(DBHelper.KEY_PNAME)));

        product.put("product_quantity", mCursor.getString(mCursor
                .getColumnIndex(DBHelper.KEY_COUNT)));

        productArray.put(contact);        



    } while (mCursor.moveToNext());

      Root.put( productArray);
}
Vishal Mokal
  • 792
  • 1
  • 5
  • 22
0
JSONArray jsArray = new JSONArray(list);

OR

Gson gson = new Gson();
String jsonArray = gson.toJson(list);

GSON

Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54
0

There is a sample from google gson documentation on how to actually convert the list to json string:

Type listType = new TypeToken<ArrayList<String>>() {}.getType();
ArrayList<String> target = new ArrayList<String>();
target.add("blah");

Gson gson = new Gson();
String json = gson.toJson(target, listType);
ArrayList<String> target2 = gson.fromJson(json, listType);

You need to set the type of list in toJson method and pass the list object to convert it to json string or vice versa.

J.K
  • 2,290
  • 1
  • 18
  • 29