C # UTF8 output keeps encoded characters intact

I have a very simple question, which I seem to be unable to omit.

I encoded UTF8-String correctly, I am in JObject with Json.NET, I script with some values ​​and write it to the command line, keeping the encoded characters intact.

Everything works fine, except for keeping the encoded characters intact.

the code:

var json = "{roster: [[\"Tulg\u00f4r\", 990, 1055]]}";
var j = JObject.Parse(json);
for (int i = 0; i < j["roster"].Count(); i++)
{
    j["roster"][i][1] = ((int)j["roster"][i][1]) * 3;
    j["roster"][i][2] = ((int)j["roster"][i][2]) * 3;
}
Console.WriteLine(JsonConvert.SerializeObject(j, Formatting.None));

Actual output:

{"roster":[["Tulgôr",2970,3165]]}

Output Required:

{"roster":[["Tulg\u00f4r",2970,3165]]}

It seems that my phrasing in Google is inappropriate, since nothing useful has come. I'm sure this is something uber-easy, and after that I will feel stupid. :)

+3
source share
2 answers

JsonConvert.SerializeObject , , ASCII, ( "\ uHHHH" ). .

// Replaces non-ASCII with escape sequences;
// i.e., converts "Tulgôr" to "Tulg\u00f4r".
private static string EscapeUnicode(string input)
{
    StringBuilder sb = new StringBuilder(input.Length);
    foreach (char ch in input)
    {
        if (ch <= 0x7f)
            sb.Append(ch);
        else
            sb.AppendFormat(CultureInfo.InvariantCulture, "\\u{0:x4}", (int) ch);
    }
    return sb.ToString();
}

:

Console.WriteLine(EscapeUnicode(JsonConvert.SerializeObject(j, Formatting.None)));

( , BMP , , "\ U00010000" "\ uD800\uDC00" ( - !) U +10000.)

+4

, . , \u. , "". , .Net, Unicode, UTF-8.

+1

Source: https://habr.com/ru/post/1747708/


All Articles