How I can use Realm to parse a large JSON and stor

2019-08-03 04:13发布

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.

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

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

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 :

 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

1条回答
The star\"
2楼-- · 2019-08-03 05:05

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

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

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

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

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

查看更多
登录 后发表回答