【发布时间】:2013-05-11 19:28:16
【问题描述】:
我正在使用 RestSharp 与远程服务器进行通信。我收到一个 JSON 序列化字符串,我可以将其反序列化为 c# 对象。我还能够将 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 服务的手工代理?
【问题讨论】:
标签: c# .net-4.0 json.net restsharp