我如何使用Realm来解析大型JSON并存储数据

时间:2021-01-17 16:01:29

J'm really blocked into parsing and storing data using Realm,I have a large JSON and I creat all the class models like the example of RealM.

我真的被阻止使用Realm解析和存储数据,我有一个大的JSON,我创建所有的类模型,如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$2.onResponse(ImagesActivity.java:133) at com.example.volleyapp2.ImagesActivity$ImagesFragment$2.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

这是我的错误:引起:org.json.JSONExcept ion:io.realm.exceptions.RealmException的值为0:无法在com的io.realm.Realm.createObjectFromJson(Realm.java:860)映射Json。 example.volleyapp2.ImagesActivity $ ImagesFragment.loadData(ImagesActivity.java:179)at com.example.volleyapp2.ImagesActivity $ ImagesFragment $ 2.onResponse(ImagesActivity.java:133)at com.example.volleyapp2.ImagesActivity $ ImagesFragment $ 2.onResponse( ImagesActivity.java:127)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)在android.app.Handler.dispatchMessage(Handler.java:92)android.app.Looper.loop(Looper.java:137)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 $ MethodAn dArgsCaller.run(ZygoteInit.java:1027)位于dalvik.system.NativeStart.main(本地方法)的com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)引起:org.json.JSONException: java.lang.String类型的值为0,无法转换为JSONObject

my function to view data

我查看数据的功能

 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");

            }
            }
    }

and this my function to parse JSON

这是我解析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);
        }

    }

and this my class model MyString :

这是我的班级模型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;
    }
}

I used this link : https://github.com/realm/realm-java/issues/575 to create ReamlList to remplace List and I can't modify my JSON

我使用此链接:https://github.com/realm/realm-java/issues/575创建ReamlList以重新列出List我无法修改我的JSON

1 个解决方案

#1


0  

Realm currently doesn't support primitive arrays. You JSON has this:

Realm目前不支持原始数组。你JSON有这个:

"languages" : ["fr", "en"]

“语言”:[“fr”,“en”]

For Realm to automatically map the JSON to your MyString class it would have to be converted to something like this:

要让Realm自动将JSON映射到MyString类,必须将其转换为以下内容:

"languages" : [ { "str" : "fr"} , { "str" : "en" } ]

“languages”:[{“str”:“fr”},{“str”:“en”}]

You can also find more information about this on GitHub: https://github.com/realm/realm-java/issues/575

您还可以在GitHub上找到有关此内容的更多信息:https://github.com/realm/realm-java/issues/575

#1


0  

Realm currently doesn't support primitive arrays. You JSON has this:

Realm目前不支持原始数组。你JSON有这个:

"languages" : ["fr", "en"]

“语言”:[“fr”,“en”]

For Realm to automatically map the JSON to your MyString class it would have to be converted to something like this:

要让Realm自动将JSON映射到MyString类,必须将其转换为以下内容:

"languages" : [ { "str" : "fr"} , { "str" : "en" } ]

“languages”:[{“str”:“fr”},{“str”:“en”}]

You can also find more information about this on GitHub: https://github.com/realm/realm-java/issues/575

您还可以在GitHub上找到有关此内容的更多信息:https://github.com/realm/realm-java/issues/575