2

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

任何想法我应该如何解决它?

谢谢!

4

1 回答 1

2

您的类CategoryEntity是正确的,但是在您的类中,应该调用Entity该属性以匹配 JSON 中的名称!ListCatagoriesCatagories

除此之外,为了解析 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...

于 2013-07-29T18:59:30.447 回答