我正在将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);
}
我也有这个问题。我做了如下
\u00A3
是英镑符号(£)。我用下面的代码来使用这个]
if (yourJsonString.Contains("£"))
{
yourJsonString = yourJsonString.Replace("£", "\\u00A3");
}
这对我有用。