Víctor Martín
Víctor Martín

Reputation: 3450

JSON parsing gives me JsonSyntaxException

I'm trying to convert a response String with a JSON inside to an Object, using Gson, but I'm facing an exception, that I think that have been caused by the double quotes or something.

Te result to convert is like:

    {
  "params": {
    "data": {
      "user": "USER01",
      "token": "924e24fdd200760b3bb",
      "language": "es-ES"
    },
    "path": "funds",
    "method": "POST",
    "okCallback": "SUCESS",
    "koCallback": "ERROR"
  }
}

I have a custom object to parse this response.

public class HybridParams {

    @SerializedName("method")
    @Expose
    private String method;
    @SerializedName("path")
    @Expose
    private String path;
    @SerializedName("data")
    @Expose
    private String data;
    @SerializedName("okCallback")
    @Expose
    private String okCallback;
    @SerializedName("koCallback")
    @Expose
    private String koCallback;

And I'm trying to do:

new Gson().fromJson(requestJson, HybridRequest.class);

But I'm getting and error:

Caused by: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 20 path $.params.data

Any advice?? Thanks

Upvotes: 0

Views: 133

Answers (4)

Romain Goutte-Fangeas
Romain Goutte-Fangeas

Reputation: 789

You're trying to deserialize the data object in JSON to a String object. Define a class for the Data object and change the data type in your HybridParams.

@Expose
private DataParams data;

And the Data class:

public class DataParams {

    @Expose
    private String user;
    @Expose
    private String token;
    @Expose
    private String language;
}

Upvotes: 1

Said
Said

Reputation: 689

Romain's answer is correct, but just want to say something about @SerializedName and @Expose.

You don't have to use both annotations in your case.

@SerializedName("method")
@Expose
private String method;

is the same as:

@Expose
private String method;

So, @Expose is included in @SerializedName, no need for both.

If you're fine with API property in this case 'method', no need to add @SerializedName.

If API property has a different name than you Class property, than you use @SerilizedName (and you can avoid @Expose).

@Expose has also one more role. JSON parser works in two directions, serialize and deserialize, and you can disable one direction with @Expose(serialize=false) or @Expose(deserialize=false)

Upvotes: 0

bijaykumarpun
bijaykumarpun

Reputation: 707

The data is a JSON object. But you are parsing it under String data type. Therefore, Error: Expected a STRING, but was BEGIN_OBJECT; which means the compiler was expecting a String ( as defined by YOU ), but was a BEGIN_OBJECT (as in the data). Your solution is to change either the definition, or the data so that they both match. Good luck.

Upvotes: 0

Mohanraj
Mohanraj

Reputation: 326

Your data field is object and you try cast that String so only you get this error.

You should try to achieve this,

@SerializedName("data")
    @Expose
    private YourNewObject data;

You can parse user, token and language from that YourNewObject

Upvotes: 0

Related Questions