Json传输中文时为了防止乱码,通常我们会进行Unicode编码 ,如{userID:"001",nickname:"\u65e0\u8bed\u68a6" }
下面的代码,将能完成Unicode的与普通字符的转换功能,函数是在网上找的,做个记号
private
string
U2CnCode(
string
str)
{
Match m;
Regex r = new Regex( " (?<code>\\\\u[a-z0-9]{4}) " , RegexOptions.IgnoreCase);
for (m = r.Match(str); m.Success; m = m.NextMatch())
{
string strValue = m.Result( " ${code} " ); // 代码
int CharNum = Int32.Parse(strValue.Substring( 2 , 4 ), System.Globalization.NumberStyles.HexNumber);
string ch = string .Format( " {0} " , ( char )CharNum);
str = str.Replace(strValue, ch);
}
return str;
}
{
Match m;
Regex r = new Regex( " (?<code>\\\\u[a-z0-9]{4}) " , RegexOptions.IgnoreCase);
for (m = r.Match(str); m.Success; m = m.NextMatch())
{
string strValue = m.Result( " ${code} " ); // 代码
int CharNum = Int32.Parse(strValue.Substring( 2 , 4 ), System.Globalization.NumberStyles.HexNumber);
string ch = string .Format( " {0} " , ( char )CharNum);
str = str.Replace(strValue, ch);
}
return str;
}