注意了!流程是:我根据url抓取了一个页面,然后直接就用UTF8编码了,现在我在本地是获取的一堆utf8编码过的字符串,我如何把它还原?获取的部分代码:
{"uid":"216073","name":"\u5c0f\u90aa\u9b54","email":"332382323235713@qq等等......
这是部分代码。
我现在想把name后面的还原(他本来是UTF8编码但此时已变成字符串了)。
那位牛B人士能把他还原~?我已经搜了很多同类问题都不能解决~~~~那个大虾~~~快来啊~~~~
{"uid":"216073","name":"\u5c0f\u90aa\u9b54","email":"332382323235713@qq等等......
这是部分代码。
我现在想把name后面的还原(他本来是UTF8编码但此时已变成字符串了)。
那位牛B人士能把他还原~?我已经搜了很多同类问题都不能解决~~~~那个大虾~~~快来啊~~~~
\b
\f
\n
\r
\t
\'
\"
\\
\ddd
\xhh
\uhhhh
非转换的忽略第一个\,比如\a就是a
string str = @"{""uid"":""216073"",""name"":""\u5c0f\u90aa\u9b54"",""email"":""332382323235713@qq等等......";
str = Regex.Replace(str, @"\\u([\da-f]{4})", m=>((char)Convert.ToInt32(m.Groups[1].Value,16)).ToString());
Console.WriteLine(str);
Match matchName = regexName.Match(m.Groups[0].Value);
if (matchName.Success)
{
if (!string.IsNullOrEmpty(matchName.Groups[1].Value))
{ string Name = HttpContext.Current.Server.UrlDecode( matchName.Groups[1].Value);
[1].Value);
}}
这里边matchName.Groups[1].Value在VS编译器里边鼠标滑动上显示是:"\\u5c0f\\u90aa\\u9b54",
但是他的值是:"\u5c0f\u90aa\u9b54“,
如果将string str="\u5c0f\u90aa\u9b54" 直接解码,就能得出中文。
问题出在哪里?
前面没加@时,单纯一个\是转义符,\\才表示\字符串本身
C#是以Unicode来保存字符串的
/**
*
* UTF-8 data encode / decode
* http://www.webtoolkit.info/
*
**/
var Utf8 = {
// public method for url encoding
encode : function (string) {
string = string.replace(/\r\n/g,"\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
},
// public method for url decoding
decode : function (utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;
while ( i < utftext.length ) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
}
问题已解决~很简单,用:
Regex.Unescape(str);
分就当散财分吧~~来顶的都有分~~知道散完为止~~~
这个可以用HTMLDncode解码出来