Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Flurl:无法序列化从HttpTest接收的JSON字符串_C#_Flurl - Fatal编程技术网

C# Flurl:无法序列化从HttpTest接收的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

我希望从RESTAPI接收JSON并将其转换为POCOs。本来应该很简单,但结果却不是:(

在我的单元测试中,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();
但它总是失败,在测试资源管理器中出现一个非常常见的错误:

结果消息: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);