Redis 将“\”添加到 JSON 字符串
Redis adds "\" to JSON string
我正在使用 ServiceStack Redis 保存 JSON 的值并稍后检索它。
问题是当我从 redis 中检索值时,它添加了多个“\”,这破坏了我的 JSON.
例如我保存:
{ "user": "123456", "password": "xxxxxxx" }
当我检索到它的值时:
{ \"user\": \"123456\", \"password\": \"xxxxxxx\" }
我想知道是否有某种设置可以防止这种情况发生?为什么会这样?
JSON 包含一些“\n”字符,这些字符也添加了“\”,我只是将它们从示例中删除以使其更具可读性。
检查我的代码后,我注意到我在从 Redis 返回数据(以字节数组形式返回)时将类型转换为字符串,并且添加了额外的“\”。
我对这个问题的解决方案是将我的 JSON 解码为 base64String,然后将其存储在 Redis 中,然后再解码回来。
ServiceStack Redis 客户端将复杂类型序列化为 JSON,如果您想按原样存储字符串,那么您应该只使用 API's that accept a string 而不是通用 T
类型,例如:
void SetAll(IEnumerable<string> keys, IEnumerable<string> values);
void SetAll(Dictionary<string, string> map);
void SetValues(Dictionary<string, string> map);
void SetValue(string key, string value);
void SetValue(string key, string value, TimeSpan expireIn);
bool SetValueIfNotExists(string key, string value);
bool SetValueIfExists(string key, string value);
string GetValue(string key);
string GetAndSetValue(string key, string value);
List<string> GetValues(List<string> keys);
我正在使用 ServiceStack Redis 保存 JSON 的值并稍后检索它。 问题是当我从 redis 中检索值时,它添加了多个“\”,这破坏了我的 JSON.
例如我保存:
{ "user": "123456", "password": "xxxxxxx" }
当我检索到它的值时:
{ \"user\": \"123456\", \"password\": \"xxxxxxx\" }
我想知道是否有某种设置可以防止这种情况发生?为什么会这样? JSON 包含一些“\n”字符,这些字符也添加了“\”,我只是将它们从示例中删除以使其更具可读性。
检查我的代码后,我注意到我在从 Redis 返回数据(以字节数组形式返回)时将类型转换为字符串,并且添加了额外的“\”。 我对这个问题的解决方案是将我的 JSON 解码为 base64String,然后将其存储在 Redis 中,然后再解码回来。
ServiceStack Redis 客户端将复杂类型序列化为 JSON,如果您想按原样存储字符串,那么您应该只使用 API's that accept a string 而不是通用 T
类型,例如:
void SetAll(IEnumerable<string> keys, IEnumerable<string> values);
void SetAll(Dictionary<string, string> map);
void SetValues(Dictionary<string, string> map);
void SetValue(string key, string value);
void SetValue(string key, string value, TimeSpan expireIn);
bool SetValueIfNotExists(string key, string value);
bool SetValueIfExists(string key, string value);
string GetValue(string key);
string GetAndSetValue(string key, string value);
List<string> GetValues(List<string> keys);