Object test = redisService.get("test");
JSONObject jsonObject = JSONUtil.parseObj(test);
BatteryParamInfo battery = JSONUtil.toBean(jsonObject, BatteryParamInfo.class);
通过这三个步骤就可以将redis中取出的json字符串转成指定的对象了。
还可以封装泛型,如下:
public <T> T getObject(String key,Class<T> t){
Object str = (key == null ? null : redisTemplate.opsForValue().get(key));
JSONObject jsonObject = JSONUtil.parseObj(str);
T obj= JSONUtil.toBean(jsonObject, t);
return obj;
}