Flurl:无法序列化从 HttpTest 收到的 JSON 字符串

Flurl: cannot serialize JSON string received from HttpTest

我想从我的 REST API 接收 JSON 并将其转换为 POCO。本来应该很简单,结果却不是:(

在我的单元测试中,我有 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();

但它总是失败,并在测试资源管理器中出现非常普遍的错误:

Result Message: Flurl.Http.FlurlHttpException : Request to http://api.com failed.

进一步挖掘似乎无法将字符串序列化为poco。我试过直接使用上面的字符串变量进行手动序列化,它很容易转换为我的模型 class,所以它不可能是代码结构问题。

// 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();

型号class:

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 时,您没有使用格式正确的 JSON。

我建议创建一个集合,将其序列化,然后 return 作为您的示例 JSON。

Model[] models = new []{
    new Model {
        project_name = "Mailjet Support",
        cluster_name = "24/7 Support",
        is_billable = "1",
        usedtime = "128"
    },               
    new Model{                
        project_name = "Caring",
        cluster_name = "Caring",
        is_billable = "0",
        usedtime = "320"
    },             
    new Model{               
        project_name = "Engagement",
        cluster_name = "Community",
        is_billable = "0",
        usedtime = "8"
    }
};

string mockJsonResponse = Newtonsoft.Json.JsonConvert.SerializeObject(models);

RespondWithJson 获取一个将为您序列化为 JSON 的对象,而不是已经序列化的字符串。用匿名对象表示测试响应,你应该很好:

var mockJsonResponse = new[] {
    new {
        project_name = "Mailjet Support",
        cluster_name = "24/7 Support",
        is_billable = "1",
        usedtime = "128"
    },               
    new {                
        project_name = "Caring",
        cluster_name = "Caring",
        is_billable = "0",
        usedtime = "320"
    },             
    new {               
        project_name = "Engagement",
        cluster_name = "Community",
        is_billable = "0",
        usedtime = "8"
    }
};

httpTest.RespondWithJson(mockJsonResponse);