Java的JSON操作存储List到Redis

使用jackson和json-lib都可以

 
            org.codehaus.jackson
            jackson-mapper-asl
            1.9.13
        


            net.sf.json-lib
            json-lib
            2.4
            jdk15
         

写缓存

JSONArray jsonarray = JSONArray.fromObject(result);
            String js = jsonarray.toString();
            JedisUtil.setString(key, js);


读缓存方法一:

String value=JedisUtil.getString(key);
        if (value != null) {
            ObjectMapper mapper = new ObjectMapper();  
//如果是List
            return mapper.readValue(value, List.class);
//如果是List            
JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, User.class);
List lst =  (List)mapper.readValue(value, javaType);

读缓存方法一:

String jsonString=...
ObjectMapper mapper = new ObjectMapper();  
List beanList = mapper.readValue(jsonString, new TypeReference>() {}); 


参考: Jackson 处理复杂类型(List,map)两种方法

你可能感兴趣的:(Java)