现在我正在使用C#.NET构建一个API,API处理数据,然后将结果转换为json字符串形式(而不是json对象(,但当我尝试使用poster进行测试时,响应体以escape字符串形式显示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结果?
我认为问题出在这里:
response = Request.CreateResponse(HttpStatusCode.OK, json);
在返回之前,您需要指定一个响应格式(Encoding&MediaType(
尝试将其更改为:
response.Content = new StringContent(json, Encoding.UTF8, "application/json");