0

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

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • The API that you are using is inconsistent, tell the developer of API to fix it. Although it is possible to work with this API by adding some patches, I do not recommend using an inconsistent API. – Nabin Bhandari Aug 19 '18 at 11:24
  • Possible duplicate: https://stackoverflow.com/questions/9598707/gson-throwing-expected-begin-object-but-was-begin-array – Martin Zeitler Aug 19 '18 at 16:04

3 Answers3

2

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");  
}
Mohammad Rbabah
  • 456
  • 2
  • 10
1

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

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • the json which I have provided is a part of a larger reply I have only given a small part where m getting the error – Sreyash Gaonkar Aug 19 '18 at 11:29
  • @SreyashGaonkar even if the leading `{` may have been truncated, the missing `}]` is still breaking the syntax; else GSON would not complain and it would not call it a `JsonSyntaxException`. ever tried to validate the whole response with jsonlint? – Martin Zeitler Aug 19 '18 at 11:48
  • yes I put the whole json response its showing valid – Sreyash Gaonkar Aug 19 '18 at 13:03
  • @SreyashGaonkar it reads: expected `BEGIN_OBJECT` but was `BEGIN_ARRAY` at `column 2527` ...which literally translates to: expected `{`, but found `[`. see the question/answers added above; if the JSON is valid, this is likely the reason. – Martin Zeitler Aug 19 '18 at 16:05
0

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;}
R7G
  • 1,000
  • 1
  • 10
  • 15
  • i contacted the api developer lets see what he replies – Sreyash Gaonkar Aug 19 '18 at 12:41
  • is there any way where i can neglect the dismissal object and read the remaining json using gson ??? – Sreyash Gaonkar Aug 19 '18 at 12:42
  • just try as I mention above it will parse the dismissalBy as String – R7G Aug 19 '18 at 14:17
  • then you can parse the dismissalBy where you need like this "Object json = new JSONTokener(response).nextValue(); if (json instanceof JSONObject) //you have an object else if (json instanceof JSONArray)" – R7G Aug 19 '18 at 14:24