简体   繁体   中英

gson.JsonSyntaxException: java.lang.IllegalStateException:

I am new to gson and getting this error.

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2527 path $.data.batting[0].scores[1].dismissal-by

it is because of the different json reply given by the API.

this is the json reply:

 "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"
  },

getting the error at the 2nd dismissal-by object.

the 1st dismissal-by starts with an object and the second dismissal-by object by an array.

this is the java class for the scores array

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;
}

}

the dismissal-by java class

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;
}

}

how do i fix this problem ? any help will be appreciated

in your JSON object, there is a

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

you cannot use it like this since you declare it as an array it should stay at the same type there for you need to change it to something like

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

,

to fix this issue you need to parse all JSON manually like

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

that JSON is invalid... therefore the com.google.gson.JsonSyntaxException .

it needs to start with a { and after "title": "Lancashire Thunder Innings" , there is a }] missing.

there you can check for yourself: https://jsonlint.com

The dismissal-by you are getting is Some time in array format (start with [ and end with ]) while some time it is Json object {} it will be better to resolve it from API developer. Or you can declare it as String and do parsing later where required.

public class Score__ implements Serializable {

@SerializedName("dismissal-by")
@Expose
private String dismissalBy;}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM