【问题标题】:Storing unicode characters with Realm使用 Realm 存储 unicode 字符
【发布时间】:2018-06-26 20:13:25
【问题描述】:

我想从文本文件中读取 JSON 字符串并将其对象存储到 Realm 文件中。

使用 Delphi 创建并以 UTF-8 编码的文本文件。我正在使用 Scanner 类从文本文件中读取字符串,然后从中提取一些 JSONObjects 和 JSONArrays。带有 unicode 字符的 JSON 对象和数组没有问题。我使用 Realm 对象的createAllFromJson 方法将它们放入领域文件中:

RealmConfiguration RealmConfig = new RealmConfiguration.Builder()
        .name("info.realm")
        .deleteRealmIfMigrationNeeded()
        .build();

Realm realm = Realm.getInstance(RealmConfig);
try{
    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            realm.createAllFromJson(AccountRecordObject.class, accounts);
            realm.createAllFromJson(SanadRecordObject.class, sanads);
            realm.createObjectFromJson(ConfigRecordObject.class, config);
        }
    });
}finally {
    realm.close();
}

并从 Realm 对象中读取:

RealmConfiguration RealmConfig = new RealmConfiguration.Builder()
        .name("info.realm")
        .deleteRealmIfMigrationNeeded()
        .build();

Realm realm = Realm.getInstance(RealmConfig);
try{
    TextView txt = parentView.findViewById(R.id.fileSampleTxt);
    String S = realm.where(AccountRecordObject.class).equalTo("AccNo", 300015).findFirst().getAccName();
    txt.setText(S);
}finally {
    realm.close();
}

问题是当我想从 Realm 文件中获取结果时,unicode 字符显示为“?” :

编辑

当我使用 createObject 方法而不是 createAllFromJson 并从 JSONArray 传递值时,unicode 字符没有问题:

AccountRecordObject obj = realm.createObject(AccountRecordObject.class);
obj.setAccNo(10000001);
try {
    obj.setAccName(accounts.getJSONObject(4).getString("AccName"));
}catch (JSONException E){
    E.printStackTrace();
}

unicode 字符的 createAllFromJson 和 createObjectFromJson 方法似乎存在问题

【问题讨论】:

    标签: android android-studio unicode realm


    【解决方案1】:

    Realm 可以很好地存储 unicode,因此很可能是 Android Studio 中使用的字体不支持文件中实际的字形。

    【讨论】:

    • 我使用createObject方法没有问题。我已编辑问题帖子
    猜你喜欢
    • 1970-01-01
    • 2011-02-11
    • 2018-05-02
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    • 2016-01-01
    • 2011-07-06
    • 2010-09-28
    相关资源
    最近更新 更多