c# - 如何从字典对象中获取值?

标签 c# dictionary

我有一个代码:

public ActionResult _getprice([FromBody] JsonElement selectedOptionsJson)
{
    var selectedOptions = JsonSerializer.Deserialize<JsonElement>(
        selectedOptionsJson.GetRawText());
    int ProductId = 0;
    var dictionaryOptions = new Dictionary<string, object>();
    foreach (var property in selectedOptions.EnumerateObject())
    {
        dictionaryOptions[property.Name] = GetObjectFromJsonElement(property.Value);
        if (property.Name == "productId")
        {
            ProductId = Convert.ToInt32(GetObjectFromJsonElement(property.Value));
        }
    }     
    var secondDictionary = dictionaryOptions.ElementAtOrDefault(1).Value;
    return Json(1);
}

seconddictionary包含:Screenshot

如何获取 seconddictionary 中存在的键值在List<keyvalue>属性键和值?

public class KeyValue
{
    public string Key { get; set; }
    public object Value { get; set; }
}

它是动态变体。

最佳答案

如果我理解正确的话你需要什么。您可以使用 Linq 来执行此操作:

List<KeyValue> keyValueList = dict.Select(kvp => new KeyValue{
Key = kvp.Key,
Value = kvp.Value}).ToList();

您应该有一个包含字典中每个对象的列表。

关于c# - 如何从字典对象中获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76729481/

相关文章:

python - 向 Python 字典添加重复键 ("Two Sum"问题)

javascript - 需要帮助理解这个 lisp 函数

c# - 如何通过 Threads c# 将节点添加到 TreeView 中?

c# - 如何在 Windows 10 邮件应用程序中打开带附件的新邮件

c# - gridview绑定(bind)数据

c# - 使用 TFS API 和 SSRS 以编程方式生成报告

python - 对象与字典 : how to organise a data tree?

ios - 将长 JSON 响应分配给 Dictionary 会产生线程错误

ios - 从 [String :AnyObject] to unrelated type NSMutableDictionary always fails Warning

python - 如何保留 python 字典中项目的顺序?