.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;
}
复制代码

 

posted @   新*  阅读(41)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)

喜欢请打赏

扫描二维码打赏

支付宝打赏

点击右上角即可分享
微信分享提示