GSON throwing “Expected BEGIN_OBJECT but was STRING

丶灬走出姿态 提交于 2019-12-12 16:39:01

问题


This is Json Object

[
   {
   "UserId":"demouser1",

   "Catagories":[
       {
       "CatagoryName":"Entertainment",
       "Persent":"25"
       },
       {
       "CatagoryName":"Household",
       "Persent":"25"
       },
       {
       "CatagoryName":"Movie",
       "Persent":"25"
       },
       {
       "CatagoryName":"Misc",
       "Persent":"25"
       }
   ],

   "RequestId":null,

   "ResponseStatus":false,

   "Token":null
   }

]

Used The Following approach to parse the above Json

public class CategoryEntity {

    private String CatagoryName;
    private String Persent;
    public String getCatagoryName() {
        return CatagoryName;
    }
    public void setCatagoryName(String catagoryName) {
        CatagoryName = catagoryName;
    }
    public String getPersent() {
        return Persent;
    }
    public void setPersent(String persent) {
        Persent = persent;
    }
}



import java.util.List;

public class Entity  {

    private String UserId;

    public String getUserId() {
        return UserId;
    }

    public void setUserId(String userId) {
        UserId = userId;
    }

    public List<CategoryEntity> getListCatagories() {
        return ListCatagories;
    }

    public void setListCatagories(List<CategoryEntity> listPMMCatagories) {
        ListCatagories = listPMMCatagories;
    }

    public String getRequestId() {
        return RequestId;
    }

    public void setRequestId(String requestId) {
        RequestId = requestId;
    }

    public boolean isResponseStatus() {
        return ResponseStatus;
    }

    public void setResponseStatus(boolean responseStatus) {
        ResponseStatus = responseStatus;
    }

    private List<CategoryEntity> ListCatagories;

    private String RequestId;

    private String Token;

    public String getToken() {
        return Token;
    }

    public void setToken(String token) {
        Token = token;
    }

    private boolean ResponseStatus; 

}

And Following approach to convert the json object to corresponding object

Gson gson =new Gson();

JsonPrimitive listCatagoriesElement= element.getAsJsonPrimitive();

                    System.out.println("listCatagoriesElement.getAsString()>>"+listCatagoriesElement.getAsString());

sysout prints:  listCatagoriesElement.getAsString()>>[{"UserId":"user1","ListCatagories":[{"CatagoryName":"Entertainment","Persent":"25"},{"CatagoryName":"Household","Persent":"25"},{"CatagoryName":"Movie","Persent":"25"},{"CatagoryName":"Misc","Persent":"25"}],"RequestId":null,"ResponseStatus":false,"Token":null}]

Entity entity = gson.fromJson(listCatagoriesElement, Entity.class);

Any ideas how should I fix it?

Thanks!


回答1:


Your class CategoryEntity is correct, but in your class Entity, the attribute ListCatagories should be called Catagories to match the name in the JSON!

Apart from that, in order to parse the JSON you'd better do something like this:

Gson gson = new Gson();
Type listType = new TypeToken<List<Entity>>() {}.getType();
List<Entity> entities = gson.fromJson(yourJsonString, listType);

So you'll have a List containing just one Entity object, and you can access the values just with:

String catagoryNameI = entities.get(0).getCatagories().get(i).getCatagoryName();
String persentI = entities.get(0).getCatagories().get(i).getPersent();

You have to do this because your whole JSON response is an array, surrounded by [ ... ], so you need to parse it into some List...



来源:https://stackoverflow.com/questions/17924788/gson-throwing-expected-begin-object-but-was-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!