1

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

Bachlet Tansime
  • 1,273
  • 3
  • 12
  • 17
  • There is a mismatch between your JSON and your model class. Looks like a string (fr) that should have been an object instead. Try posting your model class and some example JSON. – Christian Melchior Mar 15 '15 at 20:22
  • `public class Parameters extends RealmObject { @PrimaryKey private int id; private int account_id; private RealmList languages = new RealmList(); private String url; private String title; private String client; private int creation_date; private int modification_date; private String foreground_color; private String background_color;` ********* **and this a part of my JSON where I think the problem : {"application": {"parameters": {"id":999,"account_id":41,"languages":["fr","en"],"url":"","title":"Le Cep","client"** what I should do ? – Bachlet Tansime Mar 15 '15 at 21:24

1 Answers1

0

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

Christian Melchior
  • 19,978
  • 5
  • 62
  • 53
  • Thank's Christain, but I can't edit the JSON this my class model MyString, **@PrimaryKey private int id; private String myString; public MyString() { } public MyString(int id, String myString) { this.id = id; this.myString = myString; } getter and setter** I creat RealmList like the way in your link to remplace List so there's a solution or I must wait the next version of Realm ?? – Bachlet Tansime Mar 16 '15 at 09:46
  • If you are using GSON you can create your own deserialiser. See here for more info http://stackoverflow.com/questions/28733024/gson-deserialization-of-liststring-into-realmlistrealmstring – Christian Melchior Mar 17 '15 at 15:52