1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| @RequestMapping("updatecar") @ResponseBody public int updateUser(@RequestBody List<Car> list){ return list.size(); }
@Test public void readJsonAsClassInstanceList() throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper();
String jsonArray = "[{\"name\":\"A8L\",\"band\":\"Audi\",\"birth\":1627101288204,\"price\":900000},{\"name\":\"A8L\",\"band\":\"Audi\",\"birth\":1627101288204,\"price\":900000}]"; List<Car> list = mapper.readValue(jsonArray, List.class); for (Car car : list) { logger.info("name:{}", car.getBand()); } }
@Test public void readJsonAsClassInstanceList2() throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper();
String jsonArray = "[{\"name\":\"A8L\",\"band\":\"Audi\",\"birth\":1627101288204,\"price\":900000},{\"name\":\"A8L\",\"band\":\"Audi\",\"birth\":1627101288204,\"price\":900000}]"; JavaType type = mapper.getTypeFactory().constructParametricType(List.class, Car.class);
List<Car> list = mapper.readValue(jsonArray, type); for (Car car : list) { logger.info("name:{}", car.getName()); } }
|