Google Gson - deserialize list object? (generic type)

前端 未结 13 2278
灰色年华
灰色年华 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:02

    Here is a solution that works with a dynamically defined type. The trick is creating the proper type of of array using Array.newInstance().

        public static  List fromJsonList(String json, Class clazz) {
        Object [] array = (Object[])java.lang.reflect.Array.newInstance(clazz, 0);
        array = gson.fromJson(json, array.getClass());
        List list = new ArrayList();
        for (int i=0 ; i

提交回复
热议问题