Json to C#没有正确反序列化货币符号

问题描述 投票:1回答:1

我正在将C#web.api与.net core 2.1一起使用。我在appsetting.json文件中有一个Json属性,当通过AddOptions将其反序列化为c#时,£符号将更改为�

我可以在AddJsonOptions中添加设置以确保不更改此符号吗?

JSON“ SomeProperty”:“英镑£”,

C#属性SomeProperty =“磅�”]

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

Appsettings

{
  "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; }
    }
}

STARTUP

public void ConfigureServices(IServiceCollection services)
{
    services.AddOptions();
    services.Configure<SftpConfigListDto>(Configuration.GetSection("SftpConfigList"));
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
c# json serialization asp.net-core-2.0 web.api
1个回答
0
投票

我也有这个问题。我做了如下

\u00A3 

是英镑符号(£)。我用下面的代码来使用这个]

if (yourJsonString.Contains("£"))
{
     yourJsonString = yourJsonString.Replace("£", "\\u00A3");
}

这对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.