简体   繁体   中英

Parsing an List of Abstract objects of POJOS

I want to parse, with Gson's help, a list of abstract objects:

List<Pair<Integer, Integer>> 

Until now, I only had to parse Lists of objects, so when I had to declare it's type I used:

 new TypeToken<List<myObjectDto>>() {}.getType());

But in this case, I don't understand how I should declare this type in my Gson.fromJson method.

You just have to put your new List inside TypeToken :

Gson gson = new Gson();
Type type = new TypeToken<List<Pair<Integer, Integer>>>() {}.getType();
List<Pair<Integer, Integer>> yourList = gson.fromJson(jsonString, type);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM