Google Gson - deserialize list object? (generic type)

前端 未结 13 2336
灰色年华
灰色年华 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 09:57

    I liked the answer from kays1 but I couldn't implement it. So I built my own version using his concept.

    public class JsonListHelper{
        public static final  List getList(String json) throws Exception {
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            Type typeOfList = new TypeToken>(){}.getType();
            return gson.fromJson(json, typeOfList);
        }
    }
    

    Usage:

    List MyList= JsonListHelper.getList(jsonArrayString);
    

提交回复
热议问题