Click here to Skip to main content
14,735,523 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Json file which contains HTML text as well.
I have manipulated the text, but now i need to convert back the special characters as they were originally. (unicode "\u003c" instead of the real character "<")

Is there a builtin method for this?

Original text:
\u003cb\u003eTop Secret\u003c/b\u003e

After modification:
<b>Szigorúan Titkos</b>

After converting back the file to Json the HTML tags are displayed as they are or as entities, but not unicode as in the original file.

What I have tried:

- changing the encoding /possibly the wrong way as there was no successful result/
Updated 3-Feb-20 6:04am

Try this:
string escaped = @"\u003cb\u003eTop Secret\u003c/b\u003e";
string unescaped = Regex.Unescape(escaped);
Member 13050667 3-Feb-20 6:25am
Thank you, this will be useful as well later on, but I think this is not what I need.
Sorry if I wasn't clear enough.
Basically I want to convert back characters like "<" to "\u003c". No "<" or "&lt;", I need "\u003c"! :)
I hope this clears a bit :) sorry for the confusion, or if I misunderstand something.
Maciej Los 3-Feb-20 6:30am
:D :thumbsup:
If you're using JSON.NET, you can specify StringEscapeHandling.EscapeHtml to escape all HTML (<, >, &, ', ") and control characters (e.g. newline).

StringEscapeHandling Enumeration[^]

const string json = @"""\u003cb\u003eTop Secret\u003c/b\u003e""";

string deserialized = JsonConvert.DeserializeObject<string>(json);
// deserialized === "<b>Top Secret</b>"

var settings = new JsonSerializerSettings { StringEscapeHandling = StringEscapeHandling.EscapeHtml };
string serialized = JsonConvert.SerializeObject(deserialized, settings);
// serialized === "\u003cb\u003eTop Secret\u003c/b\u003e"

Alternatively, you could write your own JsonConverter to get more control over the serialization:
Custom JsonConverter<T>[^]
Maciej Los 3-Feb-20 11:15am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900