Correctly decode text containing strings like \ u003c or \ u00252 in C #

I have a JSon answer that contains a lot of \ u003c or \ u00252 or other similar lines inside. I need the correct function to decode these strings into corresponding characters.

+4
source share
3 answers

There are various reports of JSON string deserialization. A good general deserialization method is shown here . Below is the code below.

public static T Deserialise<T>(string json) { T obj = Activator.CreateInstance<T>(); using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); obj = (T)serializer.ReadObject(ms); // <== Your missing line return obj; } } 

After re-reading the message, if you are just looking for a way to convert a string to ASCII, check out this post . ORiginal Creadit for @Adam Sills for this code

 static string DecodeEncodedNonAsciiCharacters( string value ) { return Regex.Replace( value, @"\\u(?<Value>[a-zA-Z0-9]{4})", m => { return ((char) int.Parse( m.Groups["Value"].Value, NumberStyles.HexNumber )).ToString(); } ); } 
+3
source

Note. I assume that you have only part of the data, and not the entire JSON fragment - for example,

 string s = @"blah \u003c blah \u00252 blah"; 

If the above assumption is incorrect and you have a full JSON snippet, just use the JavaScriptSerializer to get the object from the data.

Annoyingly, HttpUtility is encoded but not decoded.

You can spoof a string into a full JSON object, although this seems a bit crowded:

 class Dummy { public string foo { get; set; } } static void Main(string[] args) { string s = @"blah \u003c blah \u00252 blah"; string json = @"{""foo"":""" + s + @"""}"; string unencoded = new JavaScriptSerializer().Deserialize<Dummy>(json).foo; } 
+2
source

I'm not sure, but I think you can build a char directly with unicode character code:

 char c='\003C'; // c|60 '<' 
0
source

All Articles