C# Flurl:无法序列化从HttpTest接收的JSON字符串
我希望从RESTAPI接收JSON并将其转换为POCOs。本来应该很简单,但结果却不是:( 在我的单元测试中,API发送了一个样本JSON数据字符串:C# Flurl:无法序列化从HttpTest接收的JSON字符串,c#,flurl,C#,Flurl,我希望从RESTAPI接收JSON并将其转换为POCOs。本来应该很简单,但结果却不是:( 在我的单元测试中,API发送了一个样本JSON数据字符串: string mockJsonResponse = @"[{ ""project_name"": ""Mailjet Support"", ""cluster_name"": ""24/7 Support"", ""is_billable"": ""1
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();
但它总是失败,在测试资源管理器中出现一个非常常见的错误:
结果消息:Flurl.Http.FlurlHttpException:请求http://api.com 失败。
进一步挖掘,它似乎无法将字符串序列化为poco。我已经尝试直接使用上面的字符串变量手动序列化,它可以轻松地转换为我的模型类,因此它不可能是代码结构问题
// same string variable above
var jsons = JsonConvert.DeserializeObject<List<Model>>(mockJsonResponse); // this runs fine
编辑
我尝试使用GetStringAsync
将其作为字符串获取,但字符串似乎不知何故损坏了。此字符串传递给JsonConvert.Deserialize()
将无法通过测试。这是Visual Studio调试器显示的内容。有许多转义字符
在尝试手动模拟json时,您没有使用格式良好的json 我建议创建一个集合,序列化它并将其作为示例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);
在尝试手动模拟json时,您没有使用格式良好的json 我建议创建一个集合,序列化它并将其作为示例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);
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);