私のREST APIからJSONを受け取り、それをPOCOに変換したいと考えています。簡単なことになっていますが、私のユニットテストではFlurl:HttpTestから受け取ったJSON文字列をシリアル化できません
を:(ないことが判明し、私はAPIが送信するサンプルJSONデータの文字列を持っています:私はから私のコードに送信
string mockJsonResponse = @"[{
""project_name"": ""Mailjet Support"",
""cluster_name"": ""24/7 Support"",
""is_billable"": ""1"",
""usedtime"": ""128""
},
{
""project_name"": ""Caring"",
""cluster_name"": ""Caring"",
""is_billable"": ""0"",
""usedtime"": ""320""
},
{
""project_name"": ""Engagement"",
""cluster_name"": ""Community"",
""is_billable"": ""0"",
""usedtime"": ""8""
}]";
HttpTestによるテスト:
httpTest.RespondWithJson(mockJsonResponse);
私は私のコードでそれを受信しようとしています:
dynamic response = "http://api.com".GetJsonListAsync();
しかし、それはいつものWi失敗テストエクスプローラでは非常に一般的なエラー目:
Result Message: Flurl.Http.FlurlHttpException : Request to http://api.com failed.
さらに、それはPOCOに文字列をシリアル化することができないようにそれはそう掘ります。私は上記の文字列変数を使って手動でシリアライズすることを試みましたが、それは私のモデルクラスに簡単に変換されるので、コード構造の問題ではありませんでした。
// same string variable above
var jsons = JsonConvert.DeserializeObject<List<Model>>(mockJsonResponse); // this runs fine
これらのすべてが失敗しました:
dynamic response = await "http://www.api.com".GetJsonAsync();
dynamic response = await "http://www.api.com".GetJsonAsync<Model>();
var response = await "http://www.api.com".GetJsonAsync<Model>();
IList<dynamic> response = await "http://www.api.com".GetJsonListAsync();
モデルクラス:
public class Model
{
public string project_name { get; set; }
public string cluster_name { get; set; }
public string is_billable { get; set; }
public string usedtime { get; set; }
}
編集 私はGetStringAsync
と、それを文字列として取得しようとした、そしてそれは、文字列のように思えます何とかしぼんだ。 JsonConvert.Deserialize<Model>()
に渡されたこの文字列は、テストに失敗します。これは、Visual Studioデバッガの表示です。エスケープ文字がたくさんあります。手動でもフォーマットされたJSONをprodusingされていないJSONをモックアップしようとして