我正在使用 RestSharp 与远程服务器通信。我收到一个 JSON 序列化字符串,我可以将其反序列化为 ac# 对象。我还能够将 json 数组反序列化为 List。但是,我希望在 WPF 绑定中使用这些对象,因此为了方便起见,我需要将它们放在 ObservableCollection 中。但是,如果我尝试将属性从 List 更改为 ObservableCollection(或 IList、ICollection 或 Collection),我会在反序列化时遇到异常。
Unable to cast object of type 'RestSharp.JsonArray' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]
底层代码真的没有什么特别之处,但无论如何都在这里:
private ObservableCollection<StationDto> stations;
[JsonProperty(PropertyName = "stations")]
public ObservableCollection<StationDto> Stations
{
get { return this.stations; }
set
{
this.stations = value;
RaisePropertyChanged(() => Stations);
}
}
我知道接口不起作用,因为 Json.net 需要一个具体的类来序列化。
我已经做了相当多的谷歌搜索,但我还没有看到解决方案。是否有一种通常用于用于 json/rest 服务的手工代理的模式?