在写这个问题的标题时,我遇到了this answer。但是,我正在寻找一种在WCF客户端中执行此操作的方法。有没有办法将JSON.Net插入WCF客户端?
雅虎!几天前刚刚更改了PlaceFinder服务。一个变化是响应不再包含Results
节点,而是现在包含Result
节点。另一个变化是,有时这个节点包含一个结果对象数组(当有2个或更多结果时),有时它包含一个结果对象(当只有1个结果时)。
有没有办法使用WCF DataMemberAttribute
对这种结构进行反序列化,适应这两种情况?
[DataContract]
public class PlaceFinderResultSet : IEnumerable<PlaceFinderResult>
{
// this works for 1 result, but fails when it is an array
[DataMember(Name = "Result")]
public PlaceFinderResult Result { get; set; }
// this works for an array of results, but fails when it is a single
[DataMember(Name = "Result")]
public List<PlaceFinderResult> { get; set; }
// note I do not use these together at the same time, but comment one out
// at a time. Other members in this class work as expected and are omitted
}
以下是Fiddler2中的两个示例响应(同样,结果的某些部分被省略):
JSON (here is a response with 1 result)
|--ResultSet
|--|--Found=1
|--|--Quality=37
|--|--Result
|--|--|city=city name
|--|--|country=country name
|--|--|otherprops=other values
JSON (here is a response with many results)
|--ResultSet
|--|--Found=2
|--|--Quality=40
|--|--Result
|--|--|{}
|--|--|--|city=city name1
|--|--|--|country=country name1
|--|--|--|otherprops=other values1
|--|--|{}
|--|--|--|city=city name2
|--|--|--|country=country name2
|--|--|--|otherprops=other values2