.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; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)