如何加载两个json文件?

时间:2016-01-20 21:38:07

标签: android json realm

我尝试加载两个文件时出错。 我的问题是如何将两个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();


        }
    }

1 个答案:

答案 0 :(得分:1)

"预期布尔值但是为NUMBER"

我猜你使用0和1表示布尔值而不是" true"和"假" json文件中的字符串。

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