Json to C# not de-serializing currency symbol correctly

Viewed 2232

I'm using C# WebApi with .NET Core 2.1. I have a JSON property in an appsetting.json file and when it's deserialized into c#, via AddOptions, the £ symbol is changed to �

Can I add a setting to AddJsonOptions to ensure this symbol isn't changed?

JSON "SomeProperty": "pound £",

C# Property SomeProperty= "pound �"

2 Answers

(Solution migrated from the question to an answer):

I solved this by opening the file in notepad and saving it with UTF-8 encoding. When saving the file in VS it retained the UTF-8 encoding. Not sure what Visual studio is doing when creating the appsetting file but it's seems to have been a problem in the past. https://github.com/aspnet/Configuration/issues/241 For the record I'm using VS2017 15.8.4 on windows 10

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);
}

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.

Related