如何在SQLite数据库中存储JSON对象?正确的方法是什么?

one place is the blob type column. if i can convert the JSON object into byte array and use Fileoutputstream

the other idea is to store in a text column as a String

import org.json.JSONObject;

JSONObject jsonObject;

public void createJSONObject(Fields fields) {
    jsonObject = new JSONObject();

    try {
        jsonObject.put("storedValue1", fields.storedValue1);
        jsonObject.put("storedValue2", fields.storedValue2);
        jsonObject.put("storedValue3", fields.storedValue3);
        jsonObject.put("storedValue4", fields.storedValue4);
        jsonObject.put("storedValue5", fields.storedValue5);
        jsonObject.put("storedValue6", fields.storedValue6);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

推荐答案

Convert JSONObject into String and save as TEXT/ VARCHAR. While retrieving the same column convert the String into JSONObject.

For example

Write into DB

String stringToBeInserted = jsonObject.toString();
//and insert this string into DB

Read from DB

String json = Read_column_value_logic_here
JSONObject jsonObject = new JSONObject(json);

Json相关问答推荐

从Json响应中为需要每个值的Post请求提取多个值

如何在PowerShell中扩展JSON中的嵌套数组

如何使用JQ打印每个根级对象键一行?

Jolt变换将字段移动到数组的每个元素中

递归解码嵌套列表(具有任意深度的列表列表)

Python 将 struct 化文本转换和筛选为对象

如何使用 jq 在连续的 json 记录流上调用操作

NiFi 使用嵌套数组将 avro 转换为 JSON 数组格式

将具有多个级别的 json 读入 DataFrame [python]

如何迭代、动态加载我的表单输入元素,然后在 React 中的表单提交上检索输入值?

在 Bash 中访问 JSON 对象 - 关联数组/列表/另一个模型

.NET CORE 3 升级 CORS 和 Json(cycle) XMLHttpRequest 错误

使用 c# 通用地展平 Json

从 HttpResponse 获取 json

在 Django 1.9 中,使用 JSONField(本机 postgres jsonb)的约定是什么?

有没有办法使用 Jackson 将 Map 转换为 JSON 表示而不写入文件?

python追加到json对象中的数组

使用 jQuery 和 JSON 填充表单?

NSURLRequest 中不支持的 URL

添加json文件注释