我正在使用杰克逊将json响应映射到POJO,但是当我尝试从对象映射器循环返回的列表时遇到问题。public List<T> getAll() { try { return mapper.readValue(url, new TypeReference<Collection<T>>() {}); } catch (IOException e) { e.printStackTrace(); } return null;}错误:Exception in thread "main" java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to br.com.realmpvp.commons.domain.User样本JSON: [{ "id": { "timestamp": 1529630399, "machineIdentifier": 8647350, "processIdentifier": 1524, "counter": 321373, "time": 1529630399000, "date": 1529630399000, "timeSecond": 1529630399 }, "cash": 0, "currentUsername": "teste6", "ip": "0.0.0.0", "geolocation": "Portugal", "password": "teste", "usernameHistory": [ "testeeeeeee", "fdsa" ] }]我想做的是: List<User> users = info.getAll(); for(User u : users){ System.out.println(u.getCurrentUsername()); }
1 回答

慕容3067478
TA贡献1773条经验 获得超3个赞
尝试这样做:
List<T> myUnits = objectMapper.readValue(json, objectMapper.getTypeFactory().
constructCollectionType(List.class, T.class));
我不确定,是否可以在此处使用泛型。
添加回答
举报
0/150
提交
取消