【问题标题】:How to convert arrayList<String> to json object如何将 arrayList<String> 转换为 json 对象
【发布时间】:2016-05-12 05:09:02
【问题描述】:

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

【问题讨论】:

  • 我非常努力地找到您在哪里创建 JSON 但i really tried a lot but could not find it
  • 需要重建你的 ArraryList 然后试试这个stackoverflow.com/a/4842019/3981656
  • 您没有展示任何 JSON 数组,因为 JSON 数组使用 []。您在第一个示例中展示的是一个使用数字键的 JSON 对象,其值也是具有 3 个命名键的 JSON 对象,因此它基本上是 Map&lt;Integer, Product&gt;

标签: java android json sqlite


【解决方案1】:

试试这个

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

【讨论】:

  • 这里的联系方式是什么?
  • 让我检查一下。
  • 需要帮助你能告诉我如何在php中解码并插入mysql
  • 对不起,我对php一无所知
【解决方案2】:
JSONArray jsArray = new JSONArray(list);

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

GSON

【讨论】:

    【解决方案3】:

    google gson documentation 提供了一个关于如何将列表实际转换为 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 字符串,反之亦然。

    【讨论】:

      猜你喜欢
      • 2013-10-16
      • 2018-03-27
      • 2014-08-28
      • 1970-01-01
      • 2012-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-21
      相关资源
      最近更新 更多