你好我有这个问题。从服务器我得到 JSON 字符串作为 unicode 转义序列,我需要将此序列转换为 unicode 字符串。我找到了一些解决方案,但任何不适用于所有 json 响应。
例如从服务器我得到这个字符串。
string encodedText="{\"DATA\":{\"idUser\":18167521,\"nick\":\"KecMessanger2\",\"photo\":\"1\",\"sex\":1,\"photoAlbums\":0,\"videoAlbums\":0,\"sefNick\":\"kecmessanger2\",\"profilPercent\":0,\"emphasis\":false,\"age\":25,\"isBlocked\":false,\"PHOTO\":{\"normal\":\"http://213.215.107.125/fotky/1816/75/n_18167521.jpg?v=1\",\"medium\":\"http://213.215.107.125/fotky/1816/75/m_18167521.jpg?v=1\",\"24x24\":\"http://213.215.107.125/fotky/1816/75/s_18167521.jpg?v=1\"},\"PLUS\":{\"active\":false,\"activeTo\":\"0000-00-00\"},\"LOCATION\":{\"idRegion\":\"1\",\"regionName\":\"Banskobystricku00fd kraj\",\"idCity\":\"109\",\"cityName\":\"Rimavsku00e1 Sobota\"},\"STATUS\":{\"isLoged\":true,\"isChating\":false,\"idChat\":0,\"roomName\":\"\",\"lastLogin\":1291898043},\"PROJECT_STATUS\":{\"photoAlbums\":0,\"photoAlbumsFavs\":0,\"videoAlbums\":0,\"videoAlbumsFavs\":0,\"videoAlbumsExts\":0,\"blogPosts\":0,\"emailNew\":0,\"postaNew\":0,\"clubInvitations\":0,\"dashboardItems\":26},\"STATUS_MESSAGE\":{\"statusMessage\":\"Nepru00edtomnu00fd.\",\"addTime\":\"1291887539\"},\"isFriend\":false,\"isIamFriend\":false}}";
jsonstring 中的 statusMessage 由Nepru00edtomnu00fd组成,在 .net unicode 字符串中是Neprítomný。
jsonstring 中的区域由.net unicode 字符串中的Banskobystricku00fd组成BanskoBystrický。
其他示例:
- Nepru00edtomnu00fd -> Neprítomný
- Banskobystricku00fd -> BanskoBystrický
- Trenu010du00edn -> 特伦钦
我需要将 unicode 转义序列转换为斯洛伐克语的 .net 字符串。
在转换时我使用了这个函数:
private static string UnicodeStringToNET(string input)
{
var regex = new Regex(@"\\[uU]([0-9A-F]{4})", RegexOptions.IgnoreCase);
return input = regex.Replace(input, match => ((char)int.Parse(match.Groups[1].Value,
NumberStyles.HexNumber)).ToString());
}
哪里可能有问题?