この質問のタイトルを書いているとき、私はたまたまこの答えに出くわしました。ただし、WCF クライアントでこれを行う方法を探しています。JSON.Net を WCF クライアントにプラグインする方法はありますか?
ヤフー!数日前にPlaceFinderサービスを変更したばかりです。変更の 1 つは、応答にノードが含まれなくなりResults
、代わりにノードが含まれるようになったことResult
です。もう 1 つの変更点は、このノードに結果オブジェクトの配列が含まれる場合 (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 で見られる 2 つの応答例を次に示します (ここでも、結果の一部が省略されています)。
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