JSON会使用WriteAsJsonAsync进行序列化。

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

JSON is getting serialized with WriteAsJsonAsync

问题

  1. 我正在调用一个API,代码如下:
  2. ```cs
  3. content = await request.Content.ReadAsStringAsync();
  4. var response = req.CreateResponse(request.StatusCode);
  5. await response.WriteAsJsonAsync(content, request.StatusCode);
  6. return response;

看起来一切正常,API正在运行,但JSON的输出如下所示:

  1. "{\"Policy_reference\":\"P12300025\"}"

完整代码如下:

  1. var data = new StringContent(requestBody, Encoding.UTF8, "application/json");
  2. var request = await GetSubmissions.PostAsync("api", data);
  3. if (request.IsSuccessStatusCode)
  4. {
  5. content = await request.Content.ReadAsStringAsync();
  6. var response = req.CreateResponse(request.StatusCode);
  7. await response.WriteAsJsonAsync(content, request.StatusCode);
  8. return response;
  9. }

更新

这已经正常工作了,因为我实现了WriteStringAsync,但我在Postman中接收到文本:

JSON会使用WriteAsJsonAsync进行序列化。

  1. <details>
  2. <summary>英文:</summary>
  3. I am calling an API with:
  4. ```cs
  5. content = await request.Content.ReadAsStringAsync();
  6. var response = req.CreateResponse(request.StatusCode);
  7. await response.WriteAsJsonAsync(content, request.StatusCode);
  8. return response;

and it seems to be ok and working but the JSON is coming out like this:

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

Full code:

  1. var data = new StringContent(requestBody, Encoding.UTF8, &quot;application/json&quot;);
  2. var request = await GetSubmissions.PostAsync(&quot;api&quot;, data);
  3. if (request.IsSuccessStatusCode)
  4. {
  5. content = await request.Content.ReadAsStringAsync();
  6. var response = req.CreateResponse(request.StatusCode);
  7. await response.WriteAsJsonAsync(content, request.StatusCode);
  8. return response;
  9. }

Update

This now working because I have implemented WriteStringAsync but I am receiving text in Postman:

JSON会使用WriteAsJsonAsync进行序列化。

答案1

得分: 2

WriteAsJsonAsync 将您提供的值编码为 JSON,因此实际上是对字符串进行了双重编码。相反,您应该使用 WriteAsync

  1. content = await request.Content.ReadAsStringAsync();
  2. var response = req.CreateResponse(request.StatusCode);
  3. await response.WriteAsync(content);
  4. 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':

  1. content = await request.Content.ReadAsStringAsync();
  2. var response = req.CreateResponse(request.StatusCode);
  3. await response.WriteAsync(content);
  4. return response;

答案2

得分: 2

您正在尝试将字符串编写为JSON,因此可能会遇到双重序列化的问题,请将其直接编写为字符串。假设您正在使用 HttpResponseDataExtensions.WriteAsJsonAsync,那么您可以尝试 HttpResponseDataExtensions.WriteStringAsync

  1. await response.WriteStringAsync(content);

或者尝试使用 HttpResponseWritingExtensions.WriteAsync

  1. await response.WriteAsync(content);

请注意,您可能需要手动设置内容类型:

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

  1. await response.WriteStringAsync(content);

Or try WriteAsync from HttpResponseWritingExtensions:

  1. await response.WriteAsync(content);

Note that possibly you will need to set the content type by hand:

  1. response.Headers.Add(&quot;Content-Type&quot;, &quot;application/json&quot;);

答案3

得分: 0

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

  1. return new ContentResult()
  2. {
  3. Content = content,
  4. ContentType = "application/json",
  5. StatusCode = 200
  6. };
英文:

You could always use this approach ...

  1. return new ContentResult()
  2. {
  3. Content = content,
  4. ContentType = &quot;application/json&quot;,
  5. StatusCode = 200
  6. };

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:

确定