How to deserialize a list using GSON or another JSON library in Java?

前端 未结 4 1217
半阙折子戏
半阙折子戏 2020-11-27 10:11

I can serialize a List in my servlet on GAE, but I can\'t deserialize it. What am I doing wrong?

This is my class Video in GAE, which is s

4条回答
  •  失恋的感觉
    2020-11-27 10:43

    Another way is to use an array as a type, e.g.:

    Video[] videoArray = gson.fromJson(json, Video[].class);
    

    This way you avoid all the hassle with the Type object, and if you really need a list you can always convert the array to a list, e.g.:

    List

    IMHO this is much more readable.


    In Kotlin this looks like this:

    Gson().fromJson(jsonString, Array

    To convert this array into List, just use .toList() method

提交回复
热议问题