我有一個需要反序列化的JSON「多級」響應,並且需要從反序列化的類結構中提取某個類的所有對象。Linq提取對象
在我使用的代碼下面,最後我發現我的結果是空的,沒有填充。
// given these two classes:
[DataContract]
public class ThingsList
{
[DataMember(Name = "status")]
public string Status { get; set; }
[DataMember(Name = "since")]
public double Since { get; set; }
[DataMember(Name = "list")]
public Dictionary<string, ThingsListItem> Items { get; set; }
public DateTime SinceDate { get { return UnixTime.ToDateTime(Since); } }
}
[DataContract]
public class ThingsListItem
{
[DataMember(Name = "url")]
public string Url { get; set; }
[DataMember(Name = "title")]
public string Title { get; set; }
}
// I can deserialize my json to this structure with:
ThingsList results = JsonConvert.DeserializeObject<ThingsList>(e.Result);
// now I need to "extract" only the ThingsListItem objects, and I'm trying this:
var theList = from item in results.Items.OfType<ThingsListItem>()
select new
{
Title = item.Title,
Url = item.Url
};
// but "theList" is not populated.
的點這裏是(我相信): - 我嘗試使用results.Items.OfType()以只提取ThingsListItem對象,在「上」類中聲明在 公共字典項目{get;組; } 行。
有什麼想法?告訴我們,如果現在還不清楚......
感謝 安德烈
項目是Dictionary而不是'ThingsListItem'。也許你的意思是'results.Items.Values.OfType '? –
2010-09-03 16:31:48