英文:
JSON is getting serialized with WriteAsJsonAsync
问题
我正在调用一个API,代码如下:
```cs
content = await request.Content.ReadAsStringAsync();
var response = req.CreateResponse(request.StatusCode);
await response.WriteAsJsonAsync(content, request.StatusCode);
return response;
看起来一切正常,API正在运行,但JSON的输出如下所示:
"{\"Policy_reference\":\"P12300025\"}"
完整代码如下:
var data = new StringContent(requestBody, Encoding.UTF8, "application/json");
var request = await GetSubmissions.PostAsync("api", data);
if (request.IsSuccessStatusCode)
{
content = await request.Content.ReadAsStringAsync();
var response = req.CreateResponse(request.StatusCode);
await response.WriteAsJsonAsync(content, request.StatusCode);
return response;
}
更新
这已经正常工作了,因为我实现了WriteStringAsync,但我在Postman中接收到文本:
<details>
<summary>英文:</summary>
I am calling an API with:
```cs
content = await request.Content.ReadAsStringAsync();
var response = req.CreateResponse(request.StatusCode);
await response.WriteAsJsonAsync(content, request.StatusCode);
return response;
and it seems to be ok and working but the JSON is coming out like this:
"{\"Policy_reference\":\"P12300025\"}"
Full code:
var data = new StringContent(requestBody, Encoding.UTF8, "application/json");
var request = await GetSubmissions.PostAsync("api", data);
if (request.IsSuccessStatusCode)
{
content = await request.Content.ReadAsStringAsync();
var response = req.CreateResponse(request.StatusCode);
await response.WriteAsJsonAsync(content, request.StatusCode);
return response;
}
Update
This now working because I have implemented WriteStringAsync but I am receiving text in Postman:
答案1
得分: 2
WriteAsJsonAsync
将您提供的值编码为 JSON,因此实际上是对字符串进行了双重编码。相反,您应该使用 WriteAsync
:
content = await request.Content.ReadAsStringAsync();
var response = req.CreateResponse(request.StatusCode);
await response.WriteAsync(content);
return response;
英文:
WriteAsJsonAsync
is encoding the value you give it to a JSON, so you are essentially double-encoding the string. Instead you should use `WriteAsync':
content = await request.Content.ReadAsStringAsync();
var response = req.CreateResponse(request.StatusCode);
await response.WriteAsync(content);
return response;
答案2
得分: 2
您正在尝试将字符串编写为JSON,因此可能会遇到双重序列化的问题,请将其直接编写为字符串。假设您正在使用 HttpResponseDataExtensions.WriteAsJsonAsync
,那么您可以尝试 HttpResponseDataExtensions.WriteStringAsync
:
await response.WriteStringAsync(content);
或者尝试使用 HttpResponseWritingExtensions.WriteAsync
:
await response.WriteAsync(content);
请注意,您可能需要手动设置内容类型:
response.Headers.Add("Content-Type", "application/json");
英文:
Your are trying to write a string as JSON, so you will encounter double serialization, just write it as string. Assuming you are using HttpResponseDataExtensions.WriteAsJsonAsync
, then you can try HttpResponseDataExtensions.WriteStringAsync
:
await response.WriteStringAsync(content);
Or try WriteAsync
from HttpResponseWritingExtensions
:
await response.WriteAsync(content);
Note that possibly you will need to set the content type by hand:
response.Headers.Add("Content-Type", "application/json");
答案3
得分: 0
你可以始终使用这种方法...
return new ContentResult()
{
Content = content,
ContentType = "application/json",
StatusCode = 200
};
英文:
You could always use this approach ...
return new ContentResult()
{
Content = content,
ContentType = "application/json",
StatusCode = 200
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论