gpt4 book ai didi

json - 有没有办法将 RedisValue[] 转换为 string[]?

转载 作者:行者123 更新时间:2023-12-02 03:40:41 24 4
gpt4 key购买 nike

我有一个从 StackExchange.Redis 客户端返回的数组 RedisValue[]。我想获取数组中的每个值(实际上是 JSON 字符串)并将它们连接在一起以获得可以返回给客户端的有效 JSON 字符串。

这就是我想做的......

var results = redis.HashGet("srch", ArrayOfRedisKeys[]);

string returnString = "[" + string.Join(results, ",") + "]";

但是,这不起作用,因为 resultsRedisValue 数组,而不是 string 数组。除了迭代 RedisValue 数组之外,是否有一种直接且高效的方法可以实现此目的?

最佳答案

目前没有,但我有 just pushed将以下扩展方法添加到 ExtensionMethods.cs 中:

    static readonly string[] nix = new string[0];
/// <summary>
/// Create an array of strings from an array of values
/// </summary>
public static string[] ToStringArray(this RedisValue[] values)
{
if (values == null) return null;
if (values.Length == 0) return nix;
return Array.ConvertAll(values, x => (string)x);
}

所以:在下一个版本中,您可以只使用 results.ToStringArray()。在此之前,您只需将上述内容复制到本地即可。

关于json - 有没有办法将 RedisValue[] 转换为 string[]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26143389/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com