gpt4 book ai didi

java - RealmDatabase中解析Json时出现IllegalStateException

转载 作者:太空宇宙 更新时间:2023-11-04 12:24:59 25 4
gpt4 key购买 nike

我使用数据库Realm,我有一个json对象,我需要从文件中解析它并保存到数据库。

  {
"pPID": 1,
"pName": "myName",
"pDesc": "myDescription",
"pTotal": 120,
"pOrder": 1,
"puoCompleted": false,
"puoProgressPercent":0,
"puoProgressCount":0,
"pCommercialAccessRule": {
"tag": "myTag",
"contents": [
"string object", "string object"
]
}
}

问题出现在对象pCommercialAccessRulle中,其中我们有一个字符串列表,不支持原始类型,这就是我创建一个对象的原因。

public class RealmString extends RealmObject{

String string;

public String getString() {
return string;
}

public void setString(String string) {
this.string = string;
}
}

在 CommercialAccessRule 类中,我创建的不是字符串列表,而是对象 RealmString

public class CommercialAccessRule extends RealmObject implements Serializable {


private String tag;
private RealmList<RealmString> contents;

public CommercialAccessRule() {
}

public String getTag() {
return tag;
}

public void setTag(String tag) {
this.tag = tag;
}

public RealmList<RealmString> getContents() {
return contents;
}

public void setContents(RealmList<RealmString> contents) {
this.contents = contents;
}

}

但我收到这样的错误

Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING
at android.util.JsonReader.expect(JsonReader.java:310) at android.util.JsonReader.beginObject(JsonReader.java:293)
at io.realm.RealmStringRealmProxy.createUsingJsonStream(RealmStringRealmProxy.java:136) at io.realm.CommercialAccessRuleRealmProxy.createUsingJsonStream(CommercialAccessRuleRealmProxy.java:390) at io.re
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRINGalm.PDataRealmProxy.createUsingJsonStream(PDataRealmProxy.java:525) at io.realm.DefaultRealmModuleMediator.createUsingJsonStream(DefaultRealmModuleMediator.java:257) at io.realm.Realm.createAllFromJson(Realm.java:435)

搜索后我得到there然后there ,但不明白这里如何实现:

InputStream stream = null;
try {
stream = getApplicationContext().getAssets().open("test_pdata.json");
} catch (IOException e) {
e.printStackTrace();
}

try {
realm.beginTransaction();
realm.createAllFromJson(PData.class, stream);
realm.commitTransaction();
} catch (IOException e) {
realm.cancelTransaction();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

这是我上面写的 PDate 类,json。

public class PData extends RealmObject implements Serializable{

private static final String TAG = PData.class.getSimpleName();

private String pName;
private String pDesc;
private boolean puoCompleted;
private int pPID;
private int pTotal;
private int pOrder;
private int puoProgressCount;
private int puoProgressPercent;
private CommercialAccessRule pCommercialAccessRule;
private String originalJson;

public PData() {
}


public String getpName() {
return pName;
}

public void setpName(String pName) {
this.pName = pName;
}

public String getpDesc() {
return pDesc;
}

public void setpDesc(String pDesc) {
this.pDesc = pDesc;
}

public boolean ispuoCompleted() {
return puoCompleted;
}

public void setpuoCompleted(boolean puoCompleted) {
this.puoCompleted = puoCompleted;
}

public int getpPID() {
return pPID;
}

public void setpPID(int pPID) {
this.pPID = pPID;
}

public int getpTotal() {
return pTotal;
}

public void setpTotal(int pTotal) {
this.pTotal = pTotal;
}

public int getpOrder() {
return pOrder;
}

public void setpOrder(int pOrder) {
this.pOrder = pOrder;
}

public int getpuoProgressCount() {
return puoProgressCount;
}

public void setpuoProgressCount(int puoProgressCount) {
this.puoProgressCount = puoProgressCount;
}

public int getPuoProgressPercent() {
return puoProgressPercent;
}

public void setPuoProgressPercent(int puoProgressPercent) {
this.puoProgressPercent = puoProgressPercent;
}

public CommercialAccessRule getCommercialAccessRule() {
return pCommercialAccessRule;
}

public void setpCommercialAccessRule(CommercialAccessRule pCommercialAccessRule) {
this.pCommercialAccessRule = pCommercialAccessRule;
}

public String getOriginalJson() {
return originalJson;
}

public void setOriginalJson(String originalJson) {
this.originalJson = originalJson;
}




@Override
public String toString() {
return "PData{" +
"pName='" + pName + '\'' +
", pDesc='" + pDesc + '\'' +
", puoCompleted=" + puoCompleted +
", pPID=" + pPID +
", pTotal=" + pTotal +
", pOrder=" + pOrder +
", puoProgressCount=" + puoProgressCount +
", puoProgressPercent=" + puoProgressPercent +
", pCommercialAccessRule=" + pCommercialAccessRule +
", originalJson='" + originalJson + '\'' +
'}';
}

}

最佳答案

如果您想为元素创建 JSON 对象,请使用 GSON。为此。

狐狸前任:

RealmObject obj  = new RealmObject();
obj.setpPID(1);
obj.setpName("myName");
obj.setpDesc(myDescription");
obj.setpTotal(120);
obj.setpOrder(1);
obj.setpuoCompleted(false);
obj.setpuoProgressPercent(0);
obj.setpuoProgressCount(0);
CommercialAccessRule innerobj = new CommercialAccessRule();
innerobj.setTag("myTag");
List<String> strList = new ArrayList<>();
strList.add("string object");
strList.add("string object");
obj.setpCommercialAccessRule(innerobj);
Gson gson = new Gson();
String JSONString = gson.toJson(obj);
RealmObject obj1 = gson.fromJson(JSONString,RealmObject.class);

JSONString 的结果将是

{"pPID":1,"pName":"myName","pDesc":"myDescription","pTotal":120,"pOrder":1,"puoCompleted":false,"puoProgressPercent":0,"puoProgressCount":0,"pCommercialAccessRule":{"tag":"myTag","contents":["字符串对象","字符串对象"]}}

使用它来验证您的JSON:https://jsonformatter.curiousconcept.com/

关于java - RealmDatabase中解析Json时出现IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38475726/

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