GSON投掷“预期BEGIN_OBJECT但是STRING

时间:2013-07-29 13:00:07

标签: json gson

这是Json对象

[
   {
   "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
   }

]

使用以下方法解析上面的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; 

}

和 以下方法将json对象转换为相应的对象

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);

任何想法我该如何解决?

谢谢!

1 个答案:

答案 0 :(得分:2)

您的班级CategoryEntity是正确的,但在您的班级Entity中,应将ListCatagories属性称为Catagories以匹配JSON中的名称!

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

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

因此,您只有List只包含一个Entity对象,您只需使用以下内容即可访问这些值:

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

你必须这样做,因为你的整个JSON响应是一个数组,被[ ... ]包围,所以你需要将它解析成一些List ......