[英]Json to C# not de-serializing currency symbol correctly
I'm using C# WebApi with .NET Core 2.1.我在 .NET Core 2.1 中使用 C# WebApi。 I have a JSON property in an appsetting.json file and when it's deserialized into c#, via AddOptions
, the £ symbol is changed to 我在 appsetting.json 文件中有一个 JSON 属性,当它反序列化为 c# 时,通过AddOptions
,£ 符号更改为
Can I add a setting to AddJsonOptions
to ensure this symbol isn't changed?我可以向AddJsonOptions
添加设置以确保此符号不会更改吗?
JSON "SomeProperty": "pound £", JSON "SomeProperty": "英镑",
C# Property SomeProperty= "pound " C# 属性 SomeProperty="磅 "
I also had this problem.我也有这个问题。 I did as follow我做了如下
\u00A3
is the Pound sign (£).是英镑符号 (£)。 I used this using below code我使用下面的代码使用它
if (yourJsonString.Contains("£"))
{
yourJsonString = yourJsonString.Replace("£", "\\u00A3");
}
This works for me.这对我有用。
(Solution migrated from the question to an answer): (解决方案从问题迁移到答案):
I solved this by opening the file in notepad and saving it with UTF-8 encoding.我通过在记事本中打开文件并将其保存为 UTF-8 编码解决了这个问题。 When saving the file in VS it retained the UTF-8 encoding.在 VS 中保存文件时,它保留了 UTF-8 编码。 Not sure what Visual studio is doing when creating the appsetting file but it's seems to have been a problem in the past.不确定 Visual Studio 在创建 appsetting 文件时在做什么,但过去似乎是一个问题。 https://github.com/aspnet/Configuration/issues/241 For the record I'm using VS2017 15.8.4 on windows 10 https://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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.