Search code examples
c#unicodeescapingsequences

convert unicode escape sequences to string


Hi I have this problem. From server I get JSON string as unicode escape sequences an I need convert this sequences to unicode string. I find some solution, but any doesn’t work for all json response.

For example from server I get this string.

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}}"; 

statusMessage in jsonstring consist Nepru00edtomnu00fd, in .net unicode string is it Neprítomný.

region in jsonstring consist Banskobystricku00fd in .net unicode string is it BanskoBystrický.

Other examples:

  1. Nepru00edtomnu00fd -> Neprítomný
  2. Banskobystricku00fd -> BanskoBystrický
  3. Trenu010du00edn -> Trenčín

I need convert unicode escape sequences to .net string in slovak language.

On converting I used this func:

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());
}

Where can be problem?


Solution

  • Your escape sequences do not start with a \ like "\u00fd" so you Regex should be only

    "[uU]([0-9A-F]{4})"
    

    ...