简体   繁体   中英

Gson error occurs when I convert json to gson object Expected BEGIN_ARRAY but was BEGIN_OBJECT

I am getting following error when I convert json to gson :

E/AndroidRuntime(1142): com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 51

I have json like this :

  {

"ResultCode": "0",
"ErrorMessage": null,
"Payload": [
    {
        "Items": [
            {
                "Title": "Star Wars"
            },
            { … },
            { … },
            { … },
            { … },
            { … },
            { … },
            { … },
            { … },
            { … },
            { … }
        ]
    },
    {
        "Items": [ … ]
    },
    {
        "Items": [ … ]
    }
]

} 

I have two classes : MResult class

 import java.util.List;

 public class MResult{
private String ErrorMessage;   
private String ResultCode;
private List<List<Item>> Payload;

public String getErrorMessage() {
    return ErrorMessage;
}
public void setErrorMessage(String errorMessage) {
    ErrorMessage = errorMessage;
}
public String getResultCode() {
    return ResultCode;
}
public void setResultCode(String resultCode) {
    ResultCode = resultCode;
}
public List<List<Item>> getPayload() {
    return Payload;
}
public void setPayload(List<List<Item>> payload) {
    Payload = payload;
}

}

Item class :

 public class Item{

private String Title;

 public String getTitle() {
    return Title;
}
public void setTitle(String title) {
    Title = title;
}

 }

I am getting the error on this line :

 MResult serviceResult = new Gson().fromJson(result,MResult.class);

You need another class to hold Items

List<Foo> Payload;

class Foo{
   List<Item> Items;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM