【问题标题】:How to load two json file?如何加载两个json文件?
【发布时间】:2016-01-20 21:38:06
【问题描述】:

我尝试加载两个文件时出错。 我的问题是如何将两个 json 文件加载到领域中。

以下是加载json文件:

private void loadJsonFromStream() throws IOException {

    InputStream stream = getAssets().open("school.json");

    realm.beginTransaction();
    try {
        realm.createAllFromJson(School.class, stream);
        realm.commitTransaction();
    } catch (IOException e) {
        // Remember to cancel the transaction if anything goes wrong.
        realm.cancelTransaction();
    } finally {
        if (stream != null) {
            stream.close();


        }
    }

}

下面正在加载设置设置文件。

private void loadJsonSettingFromStream() throws IOException {

    InputStream streamSetting = getAssets().open("setting.json");

    realm.beginTransaction();
    try {

        realm.createAllFromJson(Setting.class, streamSetting);
        realm.commitTransaction();
    } catch (IOException e) {
        // Remember to cancel the transaction if anything goes wrong.
        realm.cancelTransaction();
    } finally {
        if (streamSetting != null) {
            streamSetting.close();


        }
    }

【问题讨论】:

  • 显示您的错误可能会让其他人为您提供更好的帮助。

标签: android json realm


【解决方案1】:

“应为布尔值,但为 NUMBER”

我猜你使用 0 和 1 来表示布尔值,而不是 json 文件中的“true”和“false”字符串。

更改 pojo 或 json 的结构。如果这些不是选项,您可以轻松编写自定义解析器来将整数映射到布尔值。

【讨论】:

    猜你喜欢
    • 2016-05-25
    • 2020-11-25
    • 2017-05-08
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多