我有一个 POCO 类,如下所示:
public class Item : Asset
{
public int PlaylistId { get; set; }
public int AssetId { get; set; }
public double Duration { get; set; }
public int Order { get; set; }
}
和资产看起来像这样:
public enum AssetType
{
Image = 1,
Video,
Website
}
public class Asset
{
public int Id { get; set; }
public string Name { get; set; }
public string Filename { get; set; }
public AssetType Type { get; set; }
public string CreatedById { get; set; }
public string ModifiedById { get; set; }
[Display(Name="Created by")] public string CreatedBy { get; set; }
[Display(Name="Modified by")] public string ModifiedBy { get; set; }
}
然后我有一个如下所示的 json 文件:
{
"Items":[
{
"PlaylistId":1,
"Type":2,
"Duration":19,
"Filename":"stream1_mpeg4.avi"
},
{
"PlaylistId":1,
"Type":2,
"Duration":21,
"Filename":"stream2_mpeg4.avi"
}
]
}
最后我的代码如下所示:
public IList<Item> GetAll()
{
if (File.Exists(itemsPath))
{
using (var fs = new FileStream(itemsPath, FileMode.Open))
using (var sr = new StreamReader(fs))
{
var text = sr.ReadToEnd();
var array = JsonConvert.DeserializeObject<Item[]>(sr.ReadToEnd());
return array.ToList();
}
}
else
throw new FileNotFoundException("Unable to find the playlist, please make sure that " + itemsPath + " exists.");
}
正如我所料,文本变量包含正确的 json 字符串,但数组为空,因此array.ToList();引发错误。有谁知道我做错了什么?
提前干杯/r3plica