我試圖去通過每個屬性包含在JArray
一個dynamic
對象:Newtonsoft.Json - 動態對象屬性訪問
Newtonsoft.Json.Linq.JArray feeds = Newtonsoft.Json.Linq.JArray.Parse(response.Content);
if (feeds.Any())
{
PropertyDescriptorCollection dynamicProperties = TypeDescriptor.GetProperties(feeds.First());
foreach (dynamic feed in feeds)
{
object[] args = new object[dynamicProperties.Count];
int i = 0;
foreach (PropertyDescriptor prop in dynamicProperties)
{
args[i++] = feed.GetType().GetProperty(prop.Name).GetValue(feed, null);
}
yield return (T)Activator.CreateInstance(typeof(T), args);
}
}
當我triy訪問feed.GetType().GetProperty(prop.Name).GetValue(feed, null);
它告訴我,feed.GetType().GetProperty(prop.Name);
爲空。
JSON結構如下:
[
{
"digitalInput.field.channel":"tv",
"digitalInput.field.comment":"archive",
"count(digitalInput.field.comment)":130
}
]
有人能幫助我嗎?
您也可以添加您的JSON數據 - 否則每個人都在黑暗中拍攝。 – weismat
我不知道你爲什麼要在你的循環中再次向上移動樹。你想要達到什麼目標,並且在prop.GetValue()和prop.GetType()之外? – weismat