简体   繁体   中英

GSON deserialize a list with two different types of objects in Android

I'm saving my List with gson and when I'm reading it, I'm having problems. The problem is that the List I'm sending in has two different types of objects. One is CourseHeader and the other is CourseInfo. Here is the code I have right now:

Type type = new TypeToken<List>(){}.getType();
        List OLDcoursesArrayList = gson.fromJson(courseDataString, type);

In this piece: TypeToken< List > I can do something like this:

TypeToken<List<CourseHeader>>

But since there are two different objects in the list, only the CourseHeader ones work when I use them. I want to know if I can do something like this:

TypeToken<List<CourseHeader, CourseInfo>>

Or if I can retrieve the list some other way to get both my different objects in the list.

Okay to anyone else who might encounter this problem (parsing a list which could have two different data types of objects), then I got a bit of a hacky solution:

 Gson gson = new Gson();
        String courseDataString = courseData.getString("courseList", "");
        JsonArray courseDataArray = new JsonParser().parse(courseDataString).getAsJsonArray();
        List NEWcoursesArrayList = new ArrayList();

        for(JsonElement json : array){
            if(json.toString().contains("header")) {
                NEWcoursesArrayList.add(gson.fromJson(json, CourseHeader.class));
            }
            else{
                NEWcoursesArrayList.add(gson.fromJson(json, CourseInfo.class));
            }
        }

So what we're doing is we're getting the Json string as usual and converting it to a JsonArray. So we use the JsonParser to parse the string and then call "getAsJsonArray" to get a JsonArray. This array would look like this:

[{"header":"item","topSeperator":0},{"notheader":"item","topSeperator":0}]

After that I create my List object with no specific type.

Then I use a for loop which loops through the Json array using a JsonElement iterator and converts each element to a string. I use the string to check if it contains say for example "header", if it does then I add the item to my List using

List.add(gson.fromJson(json, OBJECTTYPE.class)

So what you can do is, you can have a variable inside of your objects which indicates what type of object they are. A variable like:

String objectType = "OBJECTTYPE";

Then when you convert each element inside of the Json array to a string, check to see if the string contains "OBJECTTYPE", if it does, then add the object like usual using .add(gson.fromJson(json iterator, the type of object.class).

It's a hacky solution, but it works if you have a List with two different data types. I'm sure there is a better solution.

I think you will have to create a List of CourseObject objects, both of which are the superclass of the CourseHeader and CourseInfo classes.

In your parsing code, you may be able to use introspection on the object being received to see which you need to instantiate it as, before it gets saved into the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM