JSON会使用WriteAsJsonAsync进行序列化。

huangapple go评论73阅读模式
英文:

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中接收到文本:

JSON会使用WriteAsJsonAsync进行序列化。


<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:

&quot;{\&quot;Policy_reference\&quot;:\&quot;P12300025\&quot;}&quot;

Full code:

var data = new StringContent(requestBody, Encoding.UTF8, &quot;application/json&quot;);
var request = await GetSubmissions.PostAsync(&quot;api&quot;, 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:

JSON会使用WriteAsJsonAsync进行序列化。

答案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(&quot;Content-Type&quot;, &quot;application/json&quot;);

答案3

得分: 0

你可以始终使用这种方法...

return new ContentResult()
{
    Content = content,
    ContentType = "application/json",
    StatusCode = 200
};
英文:

You could always use this approach ...

return new ContentResult()
{
    Content = content,
    ContentType = &quot;application/json&quot;,
    StatusCode = 200
};

huangapple
  • 本文由 发表于 2023年7月17日 21:49:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定