Reputation: 4400
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!
Upvotes: 1
Views: 1799
Reputation: 7010
Method createOrUpdateAllFromJson()
expects that your root object in json is Array
.
Your code works correctly for this json:
[{
"count": 1,
"statuses": [
{
"id": 0,
"name": "Visible",
"count": 0,
"sort_order": 0
},
{
"id": "1",
"name": "Invisible",
"count": 0,
"sort_order": "1"
}]
}]
Use createOrUpdateObjectFromJson()
for adding only one object (thanks to @EpicPandaForce).
Here is the source code of createOrUpdateAllFromJson
method
/**
* Tries to update a list of existing objects identified by their primary key with new JSON data. If an existing
* object could not be found in the Realm, a new object will be created. This must happen within a transaction.
*
* @param clazz type of {@link io.realm.RealmObject} to create or update. It must have a primary key defined.
* @param json string with an array of JSON objects.
* @throws java.lang.IllegalArgumentException if trying to update a class without a
* {@link io.realm.annotations.PrimaryKey}.
* @throws RealmException if unable to create a JSON array from the json string.
* @see #createAllFromJson(Class, String)
*/
public <E extends RealmObject> void createOrUpdateAllFromJson(Class<E> clazz, String json) {
if (clazz == null || json == null || json.length() == 0) {
return;
}
checkHasPrimaryKey(clazz);
JSONArray arr;
try {
arr = new JSONArray(json);
} catch (JSONException e) {
throw new RealmException("Could not create JSON array from string", e);
}
createOrUpdateAllFromJson(clazz, arr);
}
Upvotes: 4