英文:
How can I get value from dictionary object?
问题
以下是您要翻译的代码部分:
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` contains: [![Screenshot][1]][1]
[1]: https://i.stack.imgur.com/bCd5F.png
How can I get key value present in `seconddictionary` in `List<keyvalue>` properties key and value?
public class KeyValue
{
public string Key { get; set; }
public object Value { get; set; }
}
It is dynamic variant.
英文:
I have a code:
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);
}
How can I get key value present in seconddictionary
in List<keyvalue>
properties key and value?
public class KeyValue
{
public string Key { get; set; }
public object Value { get; set; }
}
It is dynamic variant.
答案1
得分: 3
你可以使用Linq来实现:
List<KeyValue> keyValueList = dict.Select(kvp => new KeyValue{
Key = kvp.Key,
Value = kvp.Value}).ToList();
这样你应该得到一个包含字典中所有对象的列表。
英文:
If I understand correctly what you need. You can use Linq to do so:
List<KeyValue> keyValueList = dict.Select(kvp => new KeyValue{
Key = kvp.Key,
Value = kvp.Value}).ToList();
You should have a list with every objects in your dictionary.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论