3

When I try to retrieve SQL table content in to a JSON format in C# eg: the content Baden-Württemberg is retrived as a "Baden-W\u00FCrttemberg" after JSON serilize. I try this

byte[] bytes = Encoding.UTF8.GetBytes(input);
input = Encoding.UTF8.GetString(bytes);
var output = JsonConvert.SerializeObject(input);

But I get "Baden-Württemberg" I really want like demo http://www.percederberg.net/tools/text_converter.html, The input type is plaintext, ISO-Latin-1

Baden-Württemberg

and output type is JSON/Javascript/Java - String text

"Baden-W\u00FCrttemberg"

How could I do in C# .Net

1
  • you should use Encoding.GetEncoding("iso-8859-1") , let me know if that works for you. Commented Dec 31, 2015 at 14:11

1 Answer 1

3

You can tell JSON.NET to escape all non-ASCII characters like this:

var json = JsonConvert.SerializeObject("Baden-Württemberg", new JsonSerializerSettings
{
    StringEscapeHandling = StringEscapeHandling.EscapeNonAscii
});

The value of json will then be:

"Baden-W\u00fcrttemberg"

And you can send the resultant JSON string through an ASCII-encoded channel.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.