@Test public void testGetPojo() throws IOException { HttpGet request = new HttpGet("http://localhost:33/list"); // 获取JSON数据 String response = this.httpClient.execute(request, new BasicResponseHandler()); // 处理JSON,转简单类型,集合类型转不了 /*User user = mapper.readValue(response, User.class);*/ // 处理复杂类型List<User> // 使用ObjectMapper对象获取类型工厂getTypeFactory() //List<User> users = mapper.readValue(response, mapper.getTypeFactory().constructCollectionType(List.class, User.class)); // 处理一切类型,包括集合中嵌套集合的类型 List<User> users = mapper.readValue(response, new TypeReference<List<User>>() {}); for (User user: users) { System.out.println(user); } // System.out.println(users); }
JSON处理复杂类型List<User>
最新推荐文章于 2023-12-12 10:47:22 发布