可以直接看最后面的第二种方式更简单一点。
1.先导入该依赖,一般使用这一个就行了
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.10.0</version> </dependency>
2.以下都是测试代码,注释都写在代码中了,自行理解,redis部分替换成对应的字符串即可。
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest public class MyTest { @Autowired private StringRedisTemplate redisTemplate; //存入字符串 @Test public void test01() { String name = "zhangmeili"; redisTemplate.opsForValue().set("name", name); } //对象转成json字符串存入redis @Test public void test02() throws JsonProcessingException { User u = new User(1001, "张三丰", "男", 20); //{"id":"1001","name":"张三丰",,,,,} ObjectMapper obj = new ObjectMapper(); String json = obj.writeValueAsString(u); redisTemplate.opsForValue().set("user", json); } //多个对象集合===>Json字符串 @Test public void test03() throws JsonProcessingException { User u1 = new User(1001, "张三丰", "男", 21); User u2 = new User(1002, "张五丰", "男", 22); User u3 = new User(1003, "张六丰", "男", 23); User u4 = new User(1004, "张七丰", "男", 24); List<User> list = Arrays.asList(u1, u2, u3, u4); ObjectMapper obj = new ObjectMapper(); String json = obj.writeValueAsString(list); //[{},{},{}] redisTemplate.opsForValue().set("users", json); } //获取字符串 @Test public void test04() { String name = redisTemplate.opsForValue().get("name"); System.out.println(name); } //redis中的json字符串====>一个对象 @Test public void test05() throws IOException { String json = redisTemplate.opsForValue().get("user"); ObjectMapper obj = new ObjectMapper(); User user = obj.readValue(json, User.class); System.out.println(user); } //redis中的json字符串====>多个对象list集合 @Test public void test06() throws IOException { String json = redisTemplate.opsForValue().get("users"); ObjectMapper obj = new ObjectMapper(); List<User> users = obj.readValue(json, obj.getTypeFactory().constructCollectionType(List.class, User.class)); System.out.println(users);; } //存入一个Map对象转成json字符串 @Test public void test07() throws JsonProcessingException { Map<String,String> map = new HashMap<>(); map.put("location", "上海"); map.put("date", "2019-12-24"); map.put("department","宣传部"); ObjectMapper obj = new ObjectMapper(); String json = obj.writeValueAsString(map); //{"location":"上海","name":"张三丰",,,,,} redisTemplate.opsForValue().set("map",json, 100, TimeUnit.MINUTES); } //获取一个json字符串=====>map集合 @Test public void test08() throws IOException { ObjectMapper obj = new ObjectMapper(); String json = redisTemplate.opsForValue().get("map"); Map<String, String> map = obj.readValue(json, obj.getTypeFactory().constructMapType(Map.class, String.class, String.class)); System.out.println(map); }
// JsonNode jsonNode = objectMapper.readTree(s);
// if (!"0000".equals(jsonNode.get("code").asText())) {
// throw new RuntimeException(jsonNode.get("data").asText());
// }
// LoginUser loginUser = objectMapper.treeToValue(jsonNode, LoginUser.class);
}
============================================
貌似使用阿里巴巴的fastjson更简单点:
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.37</version> </dependency>
然后使用JSONObject的众多方法,这里就放几个简单的,用法很简单
//把字符串转换为json类型 JSONObject jsonObject = JSONObject.parseObject("xxx"); Object o = jsonObject.get("xxx"); //把对象转成json格式的字符串 String s1 = JSONObject.toJSONString(map);