【发布时间】: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