0

I am trying to parse the following JSON :

{
  "Message": "The request is invalid.",
  "ModelState": {
    "": [
      "Name [email protected] is already taken.",
      "Email '[email protected]' is already taken."
    ]
  }
}

The code i used :

ErrorRequest page = gson.fromJson(response.getResponseString(), ErrorRequest.class);

But i am getting the error: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2

My POJO classes are : ErrorRequest.Java

package com.devinedesign.cleanride.domain;

import com.google.gson.annotations.SerializedName;

import java.util.ArrayList;
import java.util.List;

public class ErrorRequest
{
    @SerializedName("Message")
    private String message;

    @SerializedName("ModelState")
    private List<ModelState> modelState;

    public ErrorRequest(String message,List<ModelState> modelState)
    {
        this.message    = message;
        this.modelState = modelState;
    }

    public String getMessage()
    {
        return message;
    }

    public void setMessage(String message)
    {
        this.message = message;
    }

    public List<ModelState> getModelState()
    {
        return modelState;
    }

    public void setModelState(List<ModelState> modelState)
    {
        this.modelState = modelState;
    }
}

ModelState.Java

package com.devinedesign.cleanride.domain;

import com.google.gson.annotations.SerializedName;


public class ModelState
{
    @SerializedName("ModelState")
    private String modelState;

    public ModelState(String modelState)
    {
        this.modelState = modelState;
    }

    public String getModelState() {
        return modelState;
    }

    public void setModelState(String modelState) {
        this.modelState = modelState;
    }
}
4
  • 1
    Is your JSON supposed to have an empty key string in front of the array? Commented Mar 24, 2016 at 13:35
  • Well my client provided me with that in their API. Is that the reason why i was getting the error? Commented Mar 24, 2016 at 13:37
  • It should probably look more like { "Message": "The request is invalid.", "ModelState": [ "Name [email protected] is already taken.", "Email '[email protected]' is already taken." ] } . The empty key just seems to abstract a pretty simple json response. Commented Mar 24, 2016 at 13:39
  • That isn't the reason for that specific error, but I'm guessing it will cause an error after you fix the first one Commented Mar 24, 2016 at 13:44

2 Answers 2

2

ModelState here is an object.

"ModelState": {

Yet, you declared it as a List, so Gson is trying to parse an array.

@SerializedName("ModelState")
private List<ModelState> modelState;

You can fix that with

@SerializedName("ModelState")
private ModelState modelState;

But, then you should be careful about how this is will parsed.. This will be an object with a List<String>, but that empty key should have a value in it. Unless maybe you can do @SerializedName(""), but I'm not sure.

{
    "": [
      "Name [email protected] is already taken.",
      "Email '[email protected]' is already taken."
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

1

Ok i finally fixed my issue, and my client updated the JSON with following format

{"Message":"The request is invalid.","ModelState":{"Errors":["Name [email protected] is already taken.","Email '[email protected]' is already taken."]}}

And after that i modified my POJO's in the following way:

ErrorRequest.Java

package com.devinedesign.cleanride.domain;

import com.google.gson.annotations.SerializedName;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ErrorRequest
{
@SerializedName("Message")
private String message;

@SerializedName("ModelState")
private ModelState modelState;

public ErrorRequest(String message,ModelState modelState)
{
    this.message    = message;
    this.modelState = modelState;
}

public String getMessage()
{
    return message;
}

public void setMessage(String message)
{
    this.message = message;
}

public ModelState getModelState()
{
    return modelState;
}

public void setModelState(ModelState modelState)
{
    this.modelState = modelState;
}
}

ModelState.java

package com.devinedesign.cleanride.domain;

import com.google.gson.annotations.SerializedName;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class ModelState
{
@SerializedName("Errors")
private List<String> errors;

public ModelState(ArrayList<String> errors)
{
    this.errors = errors;
}

public List<String> getErrors() {
    return errors;
}

public void setErrors(List<String> errors) {
    this.errors = errors;
}
}

Now everything works perfectly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.