Google Gson - deserialize list object? (generic type)

前端 未结 13 2287
灰色年华
灰色年华 2020-11-22 09:42

I want to transfer a list object via Google Gson, but I don\'t know how to deserialize generic types.

What I tried after looking at this (BalusC\'s answer):

13条回答
  •  没有蜡笔的小新
    2020-11-22 10:15

    Wep, another way to achieve the same result. We use it for its readability.

    Instead of doing this hard-to-read sentence:

    Type listType = new TypeToken>(){}.getType();
    List list = new Gson().fromJson(jsonArray, listType);
    

    Create a empty class that extends a List of your object:

    public class YourClassList extends ArrayList {}
    

    And use it when parsing the JSON:

    List list = new Gson().fromJson(jsonArray, YourClassList.class);
    

提交回复
热议问题