c# - Json 到 C# 没有正确反序列化货币符号
问题描述
我将 C# WebApi 与 .NET Core 2.1 一起使用。我在 appsetting.json 文件中有一个 JSON 属性,当它反序列化为 c# 时,通过AddOptions
,£ 符号更改为 �</p>
我可以添加一个设置来AddJsonOptions
确保这个符号不会被改变吗?
JSON "SomeProperty": "英镑 £",
C# 属性 SomeProperty="pound �";
解决方案
(解决方案从问题迁移到答案):
我通过在记事本中打开文件并使用 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);
}
推荐阅读
- java - 在字符串参数中设置枚举值
- javascript - ServiceWorker 无法缓存持久资产
- wordpress - 使用 Google API 密钥在我的网站上呈现 Google Drive 视频文件
- python - Selenium 无法识别路径
- javascript - 如何在选中的输入 Emelent 上添加类?
- cucumber - Katalon Studio 项目中使用的“包含”文件夹是什么
- mongodb - 如何将 Shard mongo 集群转换为 Replica Set mongo 集群?实时迁移
- python - Python:TypeError:需要一个整数(获取类型模块)
- php - 为什么我不能使用函数来简化我的“for 循环”
- java - 如何在类路径 Spring Boot Jar 中添加 Ojdbc jar?