响应正文json包含转义字符串

时间:2018-12-14 20:59:30

标签: c# json api

现在我正在使用C#.NET构建API,该API处理数据,然后结果以json字符串形式(不是json对象)转换,但是当我尝试使用邮递员进行测试时,响应主体显示了json数据以转义字符串形式。

我的代码:

HttpResponseMessage response = new HttpResponseMessage();    

NameValueCollection param;
string proc = InformationExtractor.request(Request, out param);
string json;
Process(proc, param, out json);

response = Request.CreateResponse(HttpStatusCode.OK, json);

return response;

结果:

"[{\"PK_Employee_ID\":1,\"Name\":\"test\",\"Age\":24,\"FK_Job_ID\":1,\"Created_Date\":\"2018-12-14T17:54:43.460\",\"LastUpdate_Date\":\"2018-12-14T17:54:43.460\"}]"

如何在响应正文中转义json结果?

1 个答案:

答案 0 :(得分:2)

我认为问题出在这里:

response = Request.CreateResponse(HttpStatusCode.OK, json);

返回之前,您需要指定响应格式(编码和MediaType)。

尝试将其更改为此:

response.Content = new StringContent(json, Encoding.UTF8, "application/json");
相关问题