本文介绍了GSON投掷“预计BEGIN_OBJECT,但是是STRING的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Json Object

  

您的类 CategoryEntity 是正确的,你的类实体,属性 ListCatagories 应该被称为类别匹配JSON中的名称!



除此之外,为了解析JSON,你最好做这样的事情:

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

因此,您将有一个 List ,其中包含只需一个实体对象,并且您可以通过以下方式访问这些值:

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

您必须这么做,因为您的整个JSON响应是一个数组,由 [...] ,因此您需要将它解析为 List ...


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;

}

AndFollowing 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!

解决方案

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...

这篇关于GSON投掷“预计BEGIN_OBJECT,但是是STRING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 11:46