繁体   English   中英

如何转换arrayList<String> 到 json 对象

[英]How to convert arrayList<String> to json object

我想将 arraylist 转换为 json 对象。 我确实在 stackoverflow 中找到了所有内容,但无法得到我的答案。 这是我想要的格式。

$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"
}
}';

但我无法实现这一点,我得到的就是这样。

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

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

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

    }
}

我从 sqlite 获取数据并存储在 arraylist 中,这里是代码。

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

尝试这个

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

或者

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

GSON

google gson 文档中有一个关于如何将列表实际转换为 json 字符串的示例:

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

您需要在 toJson 方法中设置列表类型并传递列表对象以将其转换为 json 字符串,反之亦然。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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