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.
-------------------------------------
PS: I don't want to use any other JsonSerializer(such as Newtonsoft) at all for the project has used the JsonSerializer of Microsoft already. I don't want to use two different JsonSerializer or modify all the old one to the new one.
Your are not following standards patterns and practices. The problem is due to serializing the response twice. Simply, return the type and let the framework serialize the response.
[Route("Test")]
public TestModel Test()
{
return new TestModel() { AAA = "123", BBB = "привет123" };
}
Json by definition is UTF-8. This requires Unicode characters to be encoded in UTF-8. You created a class with Unicode strings. When serialized to JSON they are encoded. If the JSON strings are deserialized to Unicode strings, they will be correct.
Member
53 Points
246 Posts
Problem of JsonSerializer.Serialize with different language character
Nov 02, 2019 01:17 PM|mywatermelon|LINK
Here is my code:
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.
-------------------------------------
PS: I don't want to use any other JsonSerializer(such as Newtonsoft) at all for the project has used the JsonSerializer of Microsoft already. I don't want to use two different JsonSerializer or modify all the old one to the new one.
All-Star
53131 Points
23682 Posts
Re: Problem of JsonSerializer.Serialize with different language character
Nov 02, 2019 04:56 PM|mgebhard|LINK
Your are not following standards patterns and practices. The problem is due to serializing the response twice. Simply, return the type and let the framework serialize the response.
All-Star
58254 Points
15682 Posts
Re: Problem of JsonSerializer.Serialize with different language character
Nov 02, 2019 05:34 PM|bruce (sqlwork.com)|LINK
Json by definition is UTF-8. This requires Unicode characters to be encoded in UTF-8. You created a class with Unicode strings. When serialized to JSON they are encoded. If the JSON strings are deserialized to Unicode strings, they will be correct.
Member
53 Points
246 Posts
Re: Problem of JsonSerializer.Serialize with different language character
Nov 03, 2019 01:58 AM|mywatermelon|LINK
But how can I create a class with UTF-8? Just by the way of @mgebhard showed?
Member
53 Points
246 Posts
Re: Problem of JsonSerializer.Serialize with different language character
Nov 03, 2019 02:06 AM|mywatermelon|LINK
Well, it is so amazing that it doesn't need a JsonSerializer.Serialize in your way and it works!
It seems the JsonSerializer.Serialize no use at all. But what should the JsonSerializer.Serialize truly use for?
And also, how to DeSerialize the JSON in your way?
Thank you.
Member
53 Points
246 Posts
Re: Problem of JsonSerializer.Serialize with different language character
Nov 03, 2019 11:44 AM|mywatermelon|LINK
Now there is one more problem:
I have to get the string but not return at once. For example, I have to use the IHttpClientFactory to send them to another URL.
How can I solve it?
Thank you.
Member
53 Points
246 Posts
Re: Problem of JsonSerializer.Serialize with different language character
Nov 03, 2019 11:51 AM|mywatermelon|LINK
I solved the problem by modify the `JsonSerializer.Serialize` like this:
All-Star
53131 Points
23682 Posts
Re: Problem of JsonSerializer.Serialize with different language character
Nov 03, 2019 11:54 AM|mgebhard|LINK
You misunderstand basic data formatting in HTTP and ASP.NET Core. The first step is reading the framework documentation.
https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-3.0
Can you share code that reproduces this issue?
Member
53 Points
246 Posts
Re: Problem of JsonSerializer.Serialize with different language character
Nov 03, 2019 12:18 PM|mywatermelon|LINK
Thanks for helping me and finally I found a solution by using the JsonSerializer.Serialize also: