データを受信している API があります。その API は、その構造を制御することはできず、JSON 出力をシリアル化および逆シリアル化して、データをモデルにマップする必要があります。
JSONが名前付きプロパティで適切にフォーマットされている場合、すべてがうまく機能します。
名前付きの値がなく、int と文字列の配列だけがある場合に何ができますか? 場所の下のように
JSON のサンプルを次に示します。
{"id":"2160336","activation_date":"2013-08-01","expiration_date":"2013-08-29","title":"Practice Manager","locations":{"103":"Cambridge","107":"London"}}
次のようなモデルがあります。
public class ItemResults
{
public int Id { get; set; }
public DateTime Activation_Date { get; set; }
public DateTime Expiration_Date{ get; set; }
public string Title { get; set; }
public Location Locations { get; set; }
}
public class Location
{
public int Id { get; set; }
public string value { get; set; }
}
組み込みの ajax シリアライゼーションを使用してマッピングしています。
protected T MapRawApiResponseTo<T>( string response )
{
if ( string.IsNullOrEmpty( response ) )
{
return default( T );
}
var serialize = new JavaScriptSerializer();
return serialize.Deserialize<T>( response );
}
var results = MapRawApiResponseTo<ItemResults>(rawApiResponse);
そのため、ID と他のすべてのプロパティが取得され、マップされますが、何をしても場所をマップできないようです。
どうもありがとう