我想获取对象的值和键。首先,我将其转换,然后将其放在foreach
循环中以获取键和值。
object itemAttributes = web.javaScriptExecutor().ExecuteScript($"return window.INITIAL_STATE.items.itemAttributes")
var items = (ReadOnlyCollection<object>)itemAttributes;
foreach (Dictionary<string, object> item in items)
{
foreach (KeyValuePair<string, object> kvp in item)
{
if (Convert.ToString(kvp.Value) != null)
{
Name = Convert.ToString(kvp.Key), Value = Convert.ToString(kvp.Value) });
}
}
}
我想投射时出现错误
var items = (ReadOnlyCollection<object>)itemAttributes;
//Unable to cast object of type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' to type 'System.Collections.ObjectModel.ReadOnlyCollection`1[System.Object]'.