我有一个代码:
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
中存在的键值在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/