Deserializing json array using gson?

南笙酒味 提交于 2019-12-09 13:15:49

问题


I have my json as:

{
   "status": 200,
   "data": [
       {
            "catId": 638,
            "catName": "Helena Bonham Carter",
            "catUniqueName": "helena-bonham-carter",
            "catSlug": ""
       },
       {
        ...
       }
   ]
}

My Category model as:

public class Category {

    private double catId;
    private String catName;
    private String catUniqueName;
    private String catSlug;
}

And my gson custom deserializer is as follows:

Type listType = new TypeToken<ArrayList<Category>>(){}.getType();

Gson gson = new GsonBuilder()
            .registerTypeAdapter(listType, new CategoryDeserializer())
            .create();

private class CategoryDeserializer implements JsonDeserializer {
    @Override
    public Category deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
            throws JsonParseException {
        Gson gson = new Gson();
        return gson.fromJson(((JsonObject) json).get("data"), typeOfT);
    }
}

I am using Retrofit library which uses gson to serialize/deserialize json objects. I pass this custom gson deserializer to retrofit but it gives me an error. Can you tell me where I am going wrong while desrializing?

Error:

java.util.ArrayList cannot be cast to com.compzets.app.models.Category

Expected Result:

I want ArrayList of Category from json.


回答1:


Changing the return type of deserialize() from Category to ArrayList<Category> solved the issue. Rest of the code is correct.




回答2:


It would appear you're just trying to only extract a List of your Category objects from the JSON. You need your deserializer to do that, and use TypeToken accordingly to register your deserializer.

Here's a complete working example. Obviously you can change the class scoping as needed; I changed them to make it a single file example.

public class App 
{
    public static void main( String[] args )
    {
        String json = "{\n" +
            "   \"status\": 200,\n" +
            "   \"data\": [\n" +
            "       {\n" +
            "            \"catId\": 638,\n" +
            "            \"catName\": \"Helena Bonham Carter\",\n" +
            "            \"catUniqueName\": \"helena-bonham-carter\",\n" +
            "            \"catSlug\": \"\"\n" +
            "       }\n" +
            "   ]\n" +
            "}";

        Type listType = new TypeToken<List<Category>>(){}.getType();
        Gson gson = new GsonBuilder().registerTypeAdapter(listType, new CategoryDeserializer()).create();

        List<Category> list = gson.fromJson(json, listType);

        for (Category c : list)
        {
            System.out.println("CatId: " + c.catId);
        }


    }

    public static class CategoryDeserializer implements JsonDeserializer<List<Category>>
    {

        public List<Category> deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException
        {
            JsonArray data = je.getAsJsonObject().getAsJsonArray("data");
            ArrayList<Category> myList = new ArrayList<Category>();

            for (JsonElement e : data)
            {
                myList.add((Category)jdc.deserialize(e, Category.class));
            }

            return myList;

        }

    }

    public static class Category
    {
        public double catId;
        public String catName;
        public String catUniqueName;
        public String catSlug;
    }

}

Output:

CatId: 638.0



来源:https://stackoverflow.com/questions/27666558/deserializing-json-array-using-gson

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