gpt4 book ai didi

android - Realm Android 解析问题

转载 作者:行者123 更新时间:2023-11-29 17:24:04 25 4
gpt4 key购买 nike

我正在尝试在 Android 中使用 Realm 数据库解析和插入 JSON 响应。下面是我要结束的异常(exception):

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:

{
"count": 1,
"statuses": [
{
"id": 0,
"name": "Visible",
"count": 0,
"sort_order": 0
},
{
"id": "1",
"name": "Invisible",
"count": 0,
"sort_order": "1"
}]
}

计数.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;
}
}

这就是我尝试解析和存储 JSON 的方式:

Realm realm = Realm.getInstance(getContext());
realm.beginTransaction();
realm.createOrUpdateAllFromJson(Order.class, **"JSON STRING HERE"**);
realm.commitTransaction();

如果有人能帮我解决这个问题,那就太棒了。提前致谢!

最佳答案

方法 createOrUpdateAllFromJson() 期望您在 json 中的根对象是 Array

您的代码适用于此 json:

[{
"count": 1,
"statuses": [
{
"id": 0,
"name": "Visible",
"count": 0,
"sort_order": 0
},
{
"id": "1",
"name": "Invisible",
"count": 0,
"sort_order": "1"
}]
}]

使用 createOrUpdateObjectFromJson() 只添加一个对象(感谢@EpicPandaForce)。


这里是createOrUpdateAllFromJson方法的源码

/**
* 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);
}

关于android - Realm Android 解析问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35309443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com