【问题标题】:How to return only only specified Key's value from dictionary using TryGetValue() method如何使用 TryGetValue() 方法仅从字典中返回指定键的值
【发布时间】:2020-06-10 04:36:10
【问题描述】:

当我将它传递给方法时,我试图让我的方法返回 matchKey 的值,但该值返回为 Null。它还显示我的字典包含键和我正在寻找的值,我的 if(Items.TryGetValue(item.Key.Contains(matchKey).ToString() 返回 true 但它没有将它添加到我拥有的第二个字典中。 . 我的 Items 字典有所有的键和值,我可以返回它.. 但我试图从中提取一个键“id”及其值“80”,并添加到我的第二个字典 keyValues 和如果它包含,则返回它..

Here is the Json after Serializing 
{"UserId":"","UserId2":"","id":"80"}

public Dictionary<string, string> GetKeyAndValues(string matchKey)
{
    string data = JsonConvert.SerializeObject(dataEntries);
    Dictionary<string, string> Items = JsonConvert.DeserializeObject<Dictionary<string, string>>(data);
    Dictionary<string, string> keyValues = new Dictionary<string, string>();

    foreach (KeyValuePair<string, string> item in Items)
    {
        if (!Items.ContainsKey(item.Key))
        {
            Items.Add(item.Key.ToString(), item.Value.ToString());
        }
        var value = Items[matchKey];
        if(Items.TryGetValue(item.Key.Contains(matchKey).ToString(), out value))
        {
            keyValues.Add(matchKey, value);
            return keyValues;
        }
    }

    return Items;
}

【问题讨论】:

  • 欢迎来到 StackOverflow。我在您的代码中看到了几个问题,我想问您一些问题,以使您的代码对我更清楚。 dataEntries 是什么?为什么不直接从中获取所需的值?为什么要把它序列化成JSON,然后把这个JSON转换成Dictionary&lt;string, string&gt;,然后从中获取需要的值呢?
  • 嗨 :),dataEntries 是一个类,我在其中持有 Json 的属性或键。UserId、UserID2 和 id。我序列化 json 的原因是我可以在字典中反序列化它。我需要字典,因为我正在对服务执行一些 HTTP 调用并将我的查询参数作为字典键/对值传递。
  • 方法GetKeyAndValues 是否必须返回带有单个KeyValuePairDictionary?或者它可以返回KeyValuePair 而不是Dictionary?您是否考虑使用Reflection 通过其键找到所需的值,而不是使用当前算法ToJson -&gt; ToDictionary -&gt; FindValue
  • 可以使用的方法如下。使用Reflection 查找matchKey 所需的属性并获取其值。然后用一个 KeyValuePair 创建 Dictionary 并返回它。
  • 是的,它必须返回一个字典,因为将此字典应用为我的 queryParameter,因为我的 queryParameter 接受字典。您能否以这种方式显示此反射的用法示例..

标签: c# json api dictionary serialization


【解决方案1】:

您的代码中的主要问题在于这行代码:

if (Items.TryGetValue(item.Key.Contains(matchKey).ToString(), out value))

它不会检查带有 matchKey 键的项目是否包含在 Items 字典中。这个子表达式item.Key.Contains(matchKey) 检查matchKey 是否包含在Items 中并返回truefalse。因此,此问题行检查 false 的值为 true 的键是否包含在 Items 中。

这里是固定方法GetKeyAndValues

public static Dictionary<string, string> GetKeyAndValues(string matchKey)
{
    string data = JsonConvert.SerializeObject(dataEntries);
    Dictionary<string, string> Items = JsonConvert.DeserializeObject<Dictionary<string, string>>(data);

    // We don't need a loop to check if a key "matchKey" is contained in the "Items".
    // Items.TryGetValue(matchKey, out string value) checks if "matchKey" is contained
    // in the "Items", and if it is contained it returns an appropriate "value".
    if (Items.TryGetValue(matchKey, out string value))
    {
        // Here we create a Dictionary with a single pair (matchKey, value).
        return new Dictionary<string, string> {{matchKey, value}};
    }

    return Items;
}

这里是方法Dictionary.TryGetValue的文档的有用链接。

【讨论】:

  • 如果对您有帮助并解决了您的问题,请检查我的答案是否正确:)
  • 是的,但由于我是新来的,它会提示我这条消息“感谢您的反馈!声望低于 15 的人的投票将被记录,但不要更改公开显示的帖子分数。 "
猜你喜欢
  • 2013-07-16
  • 2019-09-10
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
相关资源
最近更新 更多