我试图去通过每个属性包含在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