我如何使用 Realm 解析大型 JSON 并存储数据
How I can use Realm to parse a large JSON and storing data
我真的无法使用 Realm 来解析和存储数据,我有一个很大的 JSON 并且我创建了所有 class 模型,就像 RealM 的例子一样。
this is my error :Caused by: org.json.JSONExcept
ion: Value fr at 0 of io.realm.exceptions.RealmException: Could not
map Json
at io.realm.Realm.createObjectFromJson(Realm.java:860)
at com.example.volleyapp2.ImagesActivity$ImagesFragment.loadData(ImagesActivity.java:179)
at com.example.volleyapp2.ImagesActivity$ImagesFragment.onResponse(ImagesActivity.java:133)
at com.example.volleyapp2.ImagesActivity$ImagesFragment.onResponse(ImagesActivity.java:127)
at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at dalvik.system.NativeStart.main(Native Method)
Caused by: org.json.JSONException: Value fr at 0 of type java.lang.String cannot be converted to JSONObject
我查看数据的函数
public void viewData(){
RealmResults<ApplicationBean> im = realm.where(ApplicationBean.class).findAll();
if(im.size()==0){Log.e("size de im = "+im.size()," ****");}
else { for (int i = 0; i <im.size() ; i++) {
Log.e("title = "+im.get(i).getId()," of pic");
}
}
}
这是我解析的函数JSON
public List<ApplicationBean> loadData(JSONObject obj) throws IOException, JSONException {
if (obj.length() == 0) {
Toast.makeText(getActivity(), "objet JSON est vide ! ", Toast.LENGTH_SHORT).show();
}
realm.beginTransaction();
realm.createObjectFromJson(ApplicationBean.class, obj);
realm.commitTransaction();
return realm.allObjects(ApplicationBean.class);
}
}
这是我的 class 模型 MyString :
public class MyString extends RealmObject {
@PrimaryKey
private int id;
private String myString;
public MyString() {
}
public MyString(int id, String myString) {
this.id = id;
this.myString = myString;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMyString() {
return myString;
}
public void setMyString(String myString) {
this.myString = myString;
}
}
我用过这个 link : https://github.com/realm/realm-java/issues/575
创建 ReamlList 来替换 List,我无法修改我的 JSON
Realm 目前不支持原始数组。你 JSON 有这个:
"languages" : ["fr", "en"]
要让 Realm 自动将 JSON 映射到您的 MyString class,必须将其转换为如下形式:
"languages" : [ { "str" : "fr"} , { "str" : "en" } ]
您还可以在 GitHub 上找到更多相关信息:https://github.com/realm/realm-java/issues/575
我真的无法使用 Realm 来解析和存储数据,我有一个很大的 JSON 并且我创建了所有 class 模型,就像 RealM 的例子一样。
this is my error :Caused by: org.json.JSONExcept ion: Value fr at 0 of io.realm.exceptions.RealmException: Could not map Json at io.realm.Realm.createObjectFromJson(Realm.java:860) at com.example.volleyapp2.ImagesActivity$ImagesFragment.loadData(ImagesActivity.java:179) at com.example.volleyapp2.ImagesActivity$ImagesFragment.onResponse(ImagesActivity.java:133) at com.example.volleyapp2.ImagesActivity$ImagesFragment.onResponse(ImagesActivity.java:127) at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65) at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4921) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) at dalvik.system.NativeStart.main(Native Method) Caused by: org.json.JSONException: Value fr at 0 of type java.lang.String cannot be converted to JSONObject
我查看数据的函数
public void viewData(){
RealmResults<ApplicationBean> im = realm.where(ApplicationBean.class).findAll();
if(im.size()==0){Log.e("size de im = "+im.size()," ****");}
else { for (int i = 0; i <im.size() ; i++) {
Log.e("title = "+im.get(i).getId()," of pic");
}
}
}
这是我解析的函数JSON
public List<ApplicationBean> loadData(JSONObject obj) throws IOException, JSONException {
if (obj.length() == 0) {
Toast.makeText(getActivity(), "objet JSON est vide ! ", Toast.LENGTH_SHORT).show();
}
realm.beginTransaction();
realm.createObjectFromJson(ApplicationBean.class, obj);
realm.commitTransaction();
return realm.allObjects(ApplicationBean.class);
}
}
这是我的 class 模型 MyString :
public class MyString extends RealmObject {
@PrimaryKey
private int id;
private String myString;
public MyString() {
}
public MyString(int id, String myString) {
this.id = id;
this.myString = myString;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMyString() {
return myString;
}
public void setMyString(String myString) {
this.myString = myString;
}
}
我用过这个 link : https://github.com/realm/realm-java/issues/575 创建 ReamlList 来替换 List,我无法修改我的 JSON
Realm 目前不支持原始数组。你 JSON 有这个:
"languages" : ["fr", "en"]
要让 Realm 自动将 JSON 映射到您的 MyString class,必须将其转换为如下形式:
"languages" : [ { "str" : "fr"} , { "str" : "en" } ]
您还可以在 GitHub 上找到更多相关信息:https://github.com/realm/realm-java/issues/575