简体   繁体   中英

C# UTF8 encoding

I have ac# program that retrieve some JSON data and use Newtonsoft JSON to Deserialize it. as i use persian chars in my program the JSON codes will be shown like this:\، \پ\ل\ا\ک .... also after i retrive the JSON data in my program this chars still show like its coded sample.but after i Deserialize it converted to ???? chars.

what should i do?

Your JSON deserializer is broken; \\uXXXX is supposed to be turned into proper characters.

To do that yourself, use this function

// Turns every occurrence of \uXXXX into a proper character
void UnencodeJSONUnicode(string str) {
    return Regex.Replace(str,
                         @"\\u(?<value>[0-9a-f]{4})",
                         match => {
                             string digits = match.Groups["value"].Value;
                             int number = int.Parse(digits, NumberStyles.HexNumber);
                             return char.ConvertFromUtf32(number);
                         });
}

(Untested code; I don't have VS available at the moment. Some exception handling would probably be nice too)

Looks like it has been JSON encoded, so you need to decode it. The DataContractJsonSerializer class can do this.

See this MSDN link for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM