.net 读取redis的json字符串
1.在redis下存在以下键testdata,值是json类型的字符串。
2.在C#使用ServiceStack.Redis读取,如果用redisClient.Get<string>("testdata");的时候结果会没有双引号。
3.一般需要根据redis的json内容生成实体类,通过redisClient.Get<NewModel>("testdata");的方式获取,代码参考:
//根据redis的json内容生成实体类 public class NewModel { public int age { get; set; } public string name { get; set; } public int girlfriend_number { get; set; } } //redis客户端 static RedisClient _redisClient; private static RedisClient redisClient { get { if (_redisClient != null) return _redisClient; _redisClient = new RedisClient("127.0.0.1", port); return _redisClient; } } //获取redis的value private static NewModel RedisGet(string key) { if (string.IsNullOrEmpty(key)) return null; NewModel obj = null; try { if (redisClient != null) { if (!redisClient.ContainsKey(key)) return null; obj = redisClient.Get<NewModel>(key); } } catch (Exception ex) { return null; } return obj; }