Trouble with Gson serializing an ArrayList of POJO's

后端 未结 1 801
予麋鹿
予麋鹿 2020-11-27 11:46

I had been planning on using simpleXML for my serialization needs, but figured I would try JSON out, to learn something new.

This is the code I am using to try and

相关标签:
1条回答
  • 2020-11-27 12:22

    You need to give Gson information on the specific generic type of List you're using (or any generic type you use with it). Particularly when deserializing JSON, it needs that information to be able to determine what type of object it should deserialize each array element to.

    Type listOfTestObject = new TypeToken<List<TestObject>>(){}.getType();
    String s = gson.toJson(list, listOfTestObject);
    List<TestObject> list2 = gson.fromJson(s, listOfTestObject);
    

    This is documented in the Gson user guide.

    0 讨论(0)
提交回复
热议问题