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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| ObjectMapper objectMapper = new ObjectMapper();
@Test public void testObject() throws JsonProcessingException { Goods goods = new Goods(1L, "一加6", 11110L, new Date());
String goodsJson = objectMapper.writeValueAsString(goods); System.out.println("goodsJson = " + goodsJson);
Goods g = objectMapper.readValue(goodsJson, Goods.class); System.out.println("g = " + g); }
@Test public void testList() throws JsonProcessingException { ArrayList<Goods> goodsList = new ArrayList<>(); goodsList.add(new Goods(1L, "一加6", 11110L, new Date())); goodsList.add(new Goods(2L, "一加7", 2222220L, new Date()));
String json = objectMapper.writeValueAsString(goodsList); System.out.println("json = " + json);
List list = objectMapper.readValue(json, new TypeReference<List<Goods>>() { }); System.out.println("list = " + list); }
@Test public void testMap() throws JsonProcessingException { HashMap<Integer, Goods> map = new HashMap<>(); map.put(1, new Goods(1L, "一加6", 11110L, new Date())); map.put(2, new Goods(2L, "一加7", 2222220L, new Date()));
String json = objectMapper.writeValueAsString(map); System.out.println("json = " + json);
Map<Integer, Goods> goodsMap = objectMapper.readValue(json, new TypeReference<Map<Integer, Goods>>() { }); System.out.println("goodsMap = " + goodsMap); }
|