我使用的是带有.NET Core 2.1的C#WebApi。我在appsetting.JSON文件中有一个JSON属性,当它通过AddOptions
反序列化为c#时,£符号被更改为�
我可以为AddJsonOptions
添加一个设置以确保此符号不会更改吗?
JSON"SomeProperty":"英镑£";,
C#属性SomeProperty=";英镑�">
(解决方案从问题迁移到答案(:
我通过在记事本中打开文件并用UTF-8编码保存来解决这个问题。在VS中保存文件时,它保留了UTF-8编码。不确定Visual studio在创建应用程序设置文件时在做什么,但这在过去似乎一直是个问题。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);
}
我也遇到了这个问题。我按照
u00A3
是英镑符号(£(。我使用了以下代码
if (yourJsonString.Contains("£"))
{
yourJsonString = yourJsonString.Replace("£", "\u00A3");
}
这对我有用。