Reputation: 365
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;
}
}
Upvotes: 0
Views: 1327
Reputation: 365
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.
Upvotes: 1
Reputation: 191681
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."
]
}
Upvotes: 2