java - 将 HashMap 数据保存到 SQLite

标签 java android json sqlite hashmap

我正在尝试将数据从 Json 保存到 SQLite 中。现在我将 Json 中的数据保存到 HashMap 中。

我已经搜索过了,据说使用 ContentValues。但我仍然不明白如何使用它。

我试着看这个问题save data to SQLite from json object using Hashmap in Android ,但这并没有多大帮助。

我可以使用任何选项将数据从 HashMap 保存到 SQLite 中吗?

这是我的代码。

MainHellobali.java

// Hashmap for ListView
ArrayList<HashMap<String, String>> all_itemList; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main_helloballi);

    all_itemList = new ArrayList<HashMap<String, String>>();
    // Calling async task to get json
    new getAllItem().execute();
}


private class getAllItem extends AsyncTask<Void, Void, Void> {


    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {

                all_item = new JSONArray(jsonStr);


                // looping through All Contacts
                for (int i = 0; i < all_item.length(); i++) {
                    JSONObject c = all_item.getJSONObject(i);

                        String item_id = c.getString(TAG_ITEM_ID);
                        String category_name = c.getString(TAG_CATEGORY_NAME);
                        String item_name = c.getString(TAG_ITEM_NAME);

                        // tmp hashmap for single contact
                        HashMap<String, String> allItem = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        allItem.put(TAG_ITEM_ID, item_id);
                        allItem.put(TAG_CATEGORY_NAME, category_name);
                        allItem.put(TAG_ITEM_NAME, item_name);  

                        // adding contact to contact list
                        all_itemList.add(allItem);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }
}

我也有 DatabasehHandler.javaAllItem.java。 如果有必要,我可以把它放在这里。

先谢谢

** 添加编辑后的代码 **

 // looping through All Contacts
                for (int i = 0; i < all_item.length(); i++) {
                    JSONObject c = all_item.getJSONObject(i);

                        String item_id = c.getString(TAG_ITEM_ID);
                        String category_name = c.getString(TAG_CATEGORY_NAME);
                        String item_name = c.getString(TAG_ITEM_NAME);

                        DatabaseHandler databaseHandler = new DatabaseHandler(this); //error here "The Constructor DatabaseHandler(MainHellobali.getAllItem) is undefined

}

最佳答案

如@birdy 所述,您可以将 JSON 数据作为字符串存储在数据库中。 在我的例子中,我已经做了你想要实现的同样的事情,在我的例子中,我刚刚创建了一个抽象数据源,它将扩展到我将在我的数据库中设置的任何 JSON 对象。 但基本上,您只需要一种将 JSONObject 转换为 ContentValues 对象的方法。

public ContentValues jsonToContentValues(JSONObject json) {
        ContentValues values = new ContentValues();
        values.put("MY_COLUMN", json.optString("MY_JSON_VALUE"));
        return values;
}

设置好内容值对象后,您只需将值插入数据库即可。

return database.insert("MY_TABLE_NAME", null, contentValues);

关于java - 将 HashMap 数据保存到 SQLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25430870/

相关文章:

python - 数据驱动的测试。使用 Selenium Python 从 .json 或 .yaml 文件读取参数

json - 如何使用 Hibernate 映射 postgresql json 数据类型?

java - 使用Java从服务器下载大文件时出错

java - Android 将闹钟设置为过去的日期

Java 可视化 - 如何在 Java 中转置 JTable?

java - 无法在 Android Studio 中运行应用程序。空指针异常

c# - 带有 JSON 字符串的 IHttpActionResult

java - Java 中以 14400 波特率通过 Comport 进行通信 - 串行通信

java - Android:从 EmojiconEditText 获取表情符号

android - 防止周末重复报警