<small id='DhQcy'></small><noframes id='DhQcy'>

        <i id='DhQcy'><tr id='DhQcy'><dt id='DhQcy'><q id='DhQcy'><span id='DhQcy'><b id='DhQcy'><form id='DhQcy'><ins id='DhQcy'></ins><ul id='DhQcy'></ul><sub id='DhQcy'></sub></form><legend id='DhQcy'></legend><bdo id='DhQcy'><pre id='DhQcy'><center id='DhQcy'></center></pre></bdo></b><th id='DhQcy'></th></span></q></dt></tr></i><div id='DhQcy'><tfoot id='DhQcy'></tfoot><dl id='DhQcy'><fieldset id='DhQcy'></fieldset></dl></div>

        <legend id='DhQcy'><style id='DhQcy'><dir id='DhQcy'><q id='DhQcy'></q></dir></style></legend>
          <bdo id='DhQcy'></bdo><ul id='DhQcy'></ul>
        <tfoot id='DhQcy'></tfoot>

        JsonSerializer.Deserialize 失败

        时间:2023-07-11
        • <i id='SSVj6'><tr id='SSVj6'><dt id='SSVj6'><q id='SSVj6'><span id='SSVj6'><b id='SSVj6'><form id='SSVj6'><ins id='SSVj6'></ins><ul id='SSVj6'></ul><sub id='SSVj6'></sub></form><legend id='SSVj6'></legend><bdo id='SSVj6'><pre id='SSVj6'><center id='SSVj6'></center></pre></bdo></b><th id='SSVj6'></th></span></q></dt></tr></i><div id='SSVj6'><tfoot id='SSVj6'></tfoot><dl id='SSVj6'><fieldset id='SSVj6'></fieldset></dl></div>

          <tfoot id='SSVj6'></tfoot>

                <tbody id='SSVj6'></tbody>
              • <small id='SSVj6'></small><noframes id='SSVj6'>

                • <bdo id='SSVj6'></bdo><ul id='SSVj6'></ul>
                  <legend id='SSVj6'><style id='SSVj6'><dir id='SSVj6'><q id='SSVj6'></q></dir></style></legend>

                  本文介绍了JsonSerializer.Deserialize 失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  考虑代码...

                  using System;
                  using System.Text.Json;
                  
                  public class Program
                  {
                      public static void Main()
                      {
                          int id = 9;
                          string str = "{"id": " + id + "}";
                          var u = JsonSerializer.Deserialize<User>(str);
                          Console.WriteLine($"User ID: {u.Id}, Correct: {id == u.Id}");  // always 0/init/default value
                      }
                  }
                  
                  
                  public class User {
                      public int Id { get; set; }
                  }
                  

                  为什么没有将数据正确反序列化到 User 对象中?我还通过 DotNetFiddle 验证了该行为,以防它是我系统本地的问题.不抛出异常.

                  Why isn't the data being deserialized properly into the User object? I also verified the behavior via DotNetFiddle in case it was an issue local to my system. No exception is thrown.

                  我的实际实现是在我 return Created("user", newUser)[ApiController] 的 [HttpPost] 操作中读取的>.它是通过 _httpClient.PostAsync 在我的 MVC/Razor 项目中调用的.我验证了 Created 返回到 PostAsync 调用时的值是正确的,但无论如何,从响应正文中解析的值仅包含默认值(实际 ID 是Guid).

                  My actual implementation is read from an [ApiController]'s [HttpPost] action after I return Created("user", newUser). It is called in my MVC/Razor project via _httpClient.PostAsync. I verified the values are correct when Created is returned to the PostAsync call, but no matter what, the value parsed from the response body contains only default values (the actual ID is a Guid).

                  我最初认为这可能是与 UTF8 相关的问题,因为那是我发布到 ApiControllerStringContent 的编码.引用了 UTF8 反序列化 这里,但我无法从 HttpContent 的 IO.Stream 到 ReadOnlySpanUtf8JsonReader.

                  I initially thought it might have been an UTF8 related issue, as that is the encoding for the StringContent I post to the ApiController. UTF8 deserialization is referenced here, but I had trouble getting from the IO.Stream of the HttpContent to a ReadOnlySpan or Utf8JsonReader.

                  我找到了这个项目在搜索时,这让我认为它应该可以按预期工作.

                  I found this project while searching, which makes me think it should work as I expected.

                  推荐答案

                  你的问题是 System.Text.Json 默认区分大小写,所以 "id": 9(全部小写)未映射到 Id 属性.从 文档:

                  Your problem is that System.Text.Json is case-sensitive by default, so "id": 9 (all lowercase) is not mapped to the Id property. From the docs:

                  不区分大小写的属性匹配

                  默认情况下,反序列化会在 JSON 和目标对象属性之间查找区分大小写的属性名称匹配.要更改该行为,请设置 JsonSerializerOptions.PropertyNameCaseInsensitivetrue:

                  By default, deserialization looks for case-sensitive property name matches between JSON and the target object properties. To change that behavior, set JsonSerializerOptions.PropertyNameCaseInsensitive to true:

                  注意:web 默认 不区分大小写.

                  Note: The web default is case-insensitive.

                  var options = new JsonSerializerOptions
                  {
                     PropertyNameCaseInsensitive = true,
                  };
                  var weatherForecast = JsonSerializer.Deserialize<WeatherForecast>(jsonString, options);
                  

                  所以你也需要这样做:

                  var u = JsonSerializer.Deserialize<User>(str, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
                  

                  演示小提琴 #1 这里.

                  您可以在 ASP.NET Core 3.0 中配置启动时的选项,如 How to set json serializer settings in asp 中所示.net 核心 3?:

                  You can configure the option on startup in ASP.NET Core 3.0 as shown in How to set json serializer settings in asp.net core 3?:

                  services.AddControllers().AddJsonOptions(options =>
                  {
                      options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
                  });
                  

                  或者,您可以申请 [JsonPropertyName("id")] 到您的模型:

                  Alternatively you could apply [JsonPropertyName("id")] to your model:

                  public class User {
                      [JsonPropertyName("id")]
                      public int Id { get; set; }
                  }
                  

                  演示小提琴 #2 这里.

                  这篇关于JsonSerializer.Deserialize 失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为特定操作创建不同的路线 下一篇:.NET Core 2.x 如何获取当前活动的本地网络 IPv4 地址?

                  相关文章

                • <legend id='n4u0M'><style id='n4u0M'><dir id='n4u0M'><q id='n4u0M'></q></dir></style></legend>

                  <tfoot id='n4u0M'></tfoot>
                • <i id='n4u0M'><tr id='n4u0M'><dt id='n4u0M'><q id='n4u0M'><span id='n4u0M'><b id='n4u0M'><form id='n4u0M'><ins id='n4u0M'></ins><ul id='n4u0M'></ul><sub id='n4u0M'></sub></form><legend id='n4u0M'></legend><bdo id='n4u0M'><pre id='n4u0M'><center id='n4u0M'></center></pre></bdo></b><th id='n4u0M'></th></span></q></dt></tr></i><div id='n4u0M'><tfoot id='n4u0M'></tfoot><dl id='n4u0M'><fieldset id='n4u0M'></fieldset></dl></div>

                  <small id='n4u0M'></small><noframes id='n4u0M'>

                        <bdo id='n4u0M'></bdo><ul id='n4u0M'></ul>