c# - Json 到 C# 没有正确反序列化货币符号

标签 c# json serialization asp.net-core-webapi asp.net-core-2.0

我将 C# WebApi 与 .NET Core 2.1 结合使用。我在 appsetting.json 文件中有一个 JSON 属性,当它通过 AddOptions 反序列化为 c# 时,£ 符号更改为 �

我可以向 AddJsonOptions 添加设置以确保此符号未更改吗?

JSON "SomeProperty": "英镑",

C# 属性 SomeProperty="磅�"

最佳答案

(解决方案从问题迁移到答案):

我通过在记事本中打开文件并使用 UTF-8 编码保存它来解决这个问题。在 VS 中保存文件时,它保留了 UTF-8 编码。不确定 Visual Studio 在创建 appsetting 文件时在做什么,但这在过去似乎是个问题。 https://github.com/aspnet/Configuration/issues/241作为记录,我在 Windows 10 上使用 VS2017 15.8.4

应用程序设置

{
  "SftpConfigList": {
    "SftpConfigs": [
      {
        "Host": "somehost1",
        "Username": "foo",
        "Password": "okokokok"
      },
      {
        "Host": "somehost2",
        "Username": "bar",
        "Password": "pass£££word"
      }
    ]
  }
}


namespace WebApplication1
{
    public class SftpConfigListDto
    {
        public SftpConfigDto[] SftpConfigs { get; set; }
    }

    public class SftpConfigDto
    {
        public string Host { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
    }
}

启动

public void ConfigureServices(IServiceCollection services)
{
    services.AddOptions();
    services.Configure<SftpConfigListDto>(Configuration.GetSection("SftpConfigList"));
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

关于c# - Json 到 C# 没有正确反序列化货币符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53199750/

相关文章:

hibernate - 从实体 pojo 中删除 Hibernate?

c# - 管理服务 : log user into desktop, 产生一个可以与桌面交互的进程

c# - 用c#保存XML文件并保留空元素的格式

java - 在Java中,有没有办法可以从JSONObject转到JSONStringer,反之亦然?

json - 导入json文件到elasticsearch

php - 存储在 MySQLi 中的序列化多维不打印过去的第一个数组

javascript - 如何 POST 包含数组的数据对象?

c# - ASP Core HttpClientFactory 模式使用客户端证书

c# - ASP.net MVC : why is AreaRegistration. RegisterAllAreas() 这么慢?

javascript - 延迟加载 vue-i18n 中 `en.js` 或 `ja.js` 的格式是什么