I am trying to parse and insert JSON response using Realm Database in Android. Below is the exception that I am ending up with:
io.realm.exceptions.RealmException: Could not create JSON array from string
at io.realm.Realm.createOrUpdateAllFromJson(Realm.java:396)
at mobile.login.LoginFragment$1.onResponse(LoginFragment.java:218)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
JSON that I am trying to parse:
{
"count": 1,
"statuses": [
{
"id": 0,
"name": "Visible",
"count": 0,
"sort_order": 0
},
{
"id": "1",
"name": "Invisible",
"count": 0,
"sort_order": "1"
}]
}
Count.java Pojo
public class Count extends RealmObject{
@PrimaryKey
private int count;
private RealmList<Statuses> statuses;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public RealmList<Statuses> getStatuses() {
return statuses;
}
public void setStatuses(RealmList<Statuses> statuses) {
this.statuses = statuses;
}
}
Statuses.java Pojo
public class Statuses extends RealmObject{
@PrimaryKey
@SerializedName("id")
@Expose
private String id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("count")
@Expose
private Integer count;
@SerializedName("sort_order")
@Expose
private String sortOrder;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public String getSortOrder() {
return sortOrder;
}
public void setSortOrder(String sortOrder) {
this.sortOrder = sortOrder;
}
}
This is how I try to parse and store the JSON :
Realm realm = Realm.getInstance(getContext());
realm.beginTransaction();
realm.createOrUpdateAllFromJson(Order.class, **"JSON STRING HERE"**);
realm.commitTransaction();
If someone could help me resolve this than thats really awesome. Thanks in advance!