简体   繁体   中英

How to convert arrayList<String> to json object

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());
    }

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);
}
JSONArray jsArray = new JSONArray(list);

OR

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

GSON

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM