gpt4 book ai didi

java - gson.JsonSyntaxException : java. lang.IllegalStateException:

转载 作者:行者123 更新时间:2023-12-01 19:49:33 26 4
gpt4 key购买 nike

我是 gson 新手并收到此错误。

com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为 BEGIN_OBJECT,但在第 1 行第 2527 列路径 $.data.batting[0].scores[1].dismissal-by 处为 BEGIN_ARRAY

这是因为API给出的json回复不同。

这是 json 回复:

 "batting": [
{
"scores": [
{
"dismissal-by": {
"name": "CE Rudd",
"pid": "646213"
},
"dismissal": "stumped",
"SR": 126,
"6s": 0,
"4s": 5,
"B": 34,
"R": 43,
"dismissal-info": "st Rudd b Kerr",
"batsman": "NE Bolton",
"pid": "267611"
},
{
"dismissal-by": [
{
"name": "M du Preez",
"pid": "54646"
}
],
"dismissal": "runout",
"SR": 112,
"6s": 0,
"4s": 4,
"B": 25,
"R": 28,
"dismissal-info": "run out (du Preez)",
"batsman": "GEB Boyce",
"pid": "874261"
},
{
"dismissal-by": {
"name": "LK Bell",
"pid": "878025"
},
"dismissal": "catch",
"SR": 100,
"6s": 0,
"4s": 2,
"B": 27,
"R": 27,
"dismissal-info": "c Bell b Scholfield",
"batsman": "AE Satterthwaite",
"pid": "233007"
},
{
"dismissal": "not out",
"SR": 220,
"6s": 2,
"4s": 5,
"B": 20,
"R": 44,
"dismissal-info": "not out",
"batsman": "H Kaur",
"pid": "372317"
},
{
"dismissal": "not out",
"SR": 100,
"6s": 0,
"4s": 1,
"B": 14,
"R": 14,
"dismissal-info": "not out",
"batsman": "E Threlkeld ",
"pid": "878035"
},
{
"SR": "",
"6s": "",
"4s": "",
"B": "",
"R": "",
"dismissal-info": "",
"detail": "6 (b 1, w 5)",
"batsman": "Extras",
"pid": 0
}
],
"title": "Lancashire Thunder Innings"
},

在第二个解雇对象处出现错误。

第一个missal-by以一个对象开始,第二个dismissal-by对象以数组开始。

这是分数数组的 java 类

public class Score__ implements Serializable {

@SerializedName("dismissal-by")
@Expose
private DismissalBy dismissalBy;
@SerializedName("dismissal")
@Expose
private String dismissal;
@SerializedName("SR")
@Expose
private String sR;
@SerializedName("6s")
@Expose
private String _6s;
@SerializedName("4s")
@Expose
private String _4s;
@SerializedName("B")
@Expose
private String b;
@SerializedName("R")
@Expose
private String r;
@SerializedName("dismissal-info")
@Expose
private String dismissalInfo;
@SerializedName("batsman")
@Expose
private String batsman;
@SerializedName("pid")
@Expose
private Integer pid;
@SerializedName("detail")
@Expose
private String detail;

public DismissalBy getDismissalBy() {
return dismissalBy;
}

public void setDismissalBy(DismissalBy dismissalBy) {
this.dismissalBy = dismissalBy;
}

public String getDismissal() {
return dismissal;
}

public void setDismissal(String dismissal) {
this.dismissal = dismissal;
}

public String getSR() {
return sR;
}

public void setSR(String sR) {
this.sR = sR;
}

public String get6s() {
return _6s;
}

public void set6s(String _6s) {
this._6s = _6s;
}

public String get4s() {
return _4s;
}

public void set4s(String _4s) {
this._4s = _4s;
}

public String getB() {
return b;
}

public void setB(String b) {
this.b = b;
}

public String getR() {
return r;
}

public void setR(String r) {
this.r = r;
}

public String getDismissalInfo() {
return dismissalInfo;
}

public void setDismissalInfo(String dismissalInfo) {
this.dismissalInfo = dismissalInfo;
}

public String getBatsman() {
return batsman;
}

public void setBatsman(String batsman) {
this.batsman = batsman;
}

public Integer getPid() {
return pid;
}

public void setPid(Integer pid) {
this.pid = pid;
}

public String getDetail() {
return detail;
}

public void setDetail(String detail) {
this.detail = detail;
}

}

java 类的解雇

public class DismissalBy implements Serializable {

@SerializedName("name")
@Expose
private String name;
@SerializedName("pid")
@Expose
private String pid;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPid() {
return pid;
}

public void setPid(String pid) {
this.pid = pid;
}

}

我该如何解决这个问题?任何帮助将不胜感激

最佳答案

在您的 JSON 对象中,有一个

"dismissal-by": {
"name": "LK Bell",
"pid": "878025"
},

你不能像这样使用它,因为你将它声明为一个数组,它应该保持相同的类型,因为你需要将它更改为类似的东西

  "dismissal-by"":[
{
"name": "LK Bell",
"pid": "878025"
}
]

要解决此问题,您需要像这样手动解析所有 JSON

Json json = new Json(string);
try{
json.getJsonArray("dismissal-by");
}catch(IllegalStateException e)
{
json.getObject("dismissal-by");
}

关于java - gson.JsonSyntaxException : java. lang.IllegalStateException:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51917105/

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