0

Here is my code:

public class TestModel{
  public string AAA{get;set;}
  public string BBB{get;set;}
}
        [Route("Test")]
        public async Task<string> Test()
        {           
            TestModel _TestModel=new TestModel(){AAA="123",BBB="привет123"};
            string JSON = JsonSerializer.Serialize(_TestModel, typeof(TestModel));            
            return JSON;
        }

When I ran the program, the JSON convert successfully.

However, the character which is not English or number can not convert correctly but only displays some strange string such as "\u65B0".

I think maybe it is the problem of encoding. However, it seems I can't set the encoding in the JsonSerializer.Serialize.

How can I solve this problem? Thank you.

ArunPratap
  • 4,816
  • 7
  • 25
  • 43
Melon NG
  • 2,568
  • 6
  • 27
  • 52

1 Answers1

1

I solved the problem by modify the JsonSerializer.Serialize like this:

string JSON = JsonSerializer.Serialize(_TestModel, typeof(TestModel), new JsonSerializerOptions() { Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Create(System.Text.Unicode.UnicodeRanges.All) });
Melon NG
  • 2,568
  • 6
  • 27
  • 52