Gson: JsonSyntaxException:

Created on 8 Jan 2018  Â·  4Comments  Â·  Source: google/gson

I have json response which changes some time

For eg: logo is the parameter which changes some time

1) actual response is will be a json object

"logo": {
}

2) Some times the response will be in string object

"logo":"5555"

So it throws exception com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1

Any Idea in gson to handle this kind of situation??

Most helpful comment

You can write a type adapter which conditionally handles both cases.

On Mon, Jan 8, 2018, 9:34 AM DineshkumarArunachalam <
[email protected]> wrote:

I have json response which changes some time

For eg: logo is the parameter which changes some time

  1. actual response is will be a json object

"logo": {
}

  1. Some times the response will be in string object

"logo":"5555"

So it throws exception com.google.gson.JsonSyntaxException:
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at
line 1

Any Idea in gson to handle this kind of situation??

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/google/gson/issues/1224, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAEEEeHYLSnVxwIFgg1EkCdaWUN1MDaRks5tIidZgaJpZM4RWdDy
.

All 4 comments

You can write a type adapter which conditionally handles both cases.

On Mon, Jan 8, 2018, 9:34 AM DineshkumarArunachalam <
[email protected]> wrote:

I have json response which changes some time

For eg: logo is the parameter which changes some time

  1. actual response is will be a json object

"logo": {
}

  1. Some times the response will be in string object

"logo":"5555"

So it throws exception com.google.gson.JsonSyntaxException:
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at
line 1

Any Idea in gson to handle this kind of situation??

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/google/gson/issues/1224, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAEEEeHYLSnVxwIFgg1EkCdaWUN1MDaRks5tIidZgaJpZM4RWdDy
.

This my predefined model class

public class Logo {
@SerializedName("xxx")
@Expose
private Integer xxx;
@SerializedName("yyy")
@Expose
private String yyy;
@SerializedName("qqq")
@Expose
private String qqq;
@SerializedName("rrr")
@Expose
private String rrr;
@SerializedName("jjj")
@Expose
private Format jjj;
@SerializedName("lll")
@Expose
private MediaType lll;
}
While change in the logo structure in response causing these exception.

can you please provide some ideas to write type adapter for handling this case. This will be helpful for me. Thanks in advance

Ask on Stack Overflow with the gson tag.

In your TypeAdapter.read, you could JsonReader.peek() and check if the token is a string. If it is a string, JsonReader.nextString(). If it not, JsonReader.beginObject(), read the object, and JsonReader.endObject().

People on SO might help.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GoogleCodeExporter picture GoogleCodeExporter  Â·  25Comments

priyankajagtap18 picture priyankajagtap18  Â·  14Comments

GoogleCodeExporter picture GoogleCodeExporter  Â·  31Comments

JakeWharton picture JakeWharton  Â·  39Comments

RobMans426 picture RobMans426  Â·  20Comments