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

时间:2015-03-15 19:01:59

标签: android json android-volley realm

我真的被阻止使用Realm解析和存储数据,我有一个很大的JSON,我创建所有的类模型,如RealM的例子。

  

这是我的错误:引起:org.json.JSONExcept   ion:io.realm.exceptions.RealmException的值为0:不能   地图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)               在com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)               在com.android.volley.ExecutorDelivery $ ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)               在android.os.Handler.handleCallback(Handler.java:615)               在android.os.Handler.dispatchMessage(Handler.java:92)               在android.os.Looper.loop(Looper.java:137)               在android.app.ActivityThread.main(ActivityThread.java:4921)               at java.lang.reflect.Method.invokeNative(Native Method)               在java.lang.reflect.Method.invoke(Method.java:511)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1027)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)               at dalvik.system.NativeStart.main(Native Method)        引起:org.json.JSONException:类型为java.lang.String的0的值fr无法转换为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);
        }

    }

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

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

1 个答案:

答案 0 :(得分:0)

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

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

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

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

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