Issues with accentuation when reading data from JSON file

Asked

Viewed 1,658 times

4

The method below is extracting data from a JSON file, to fill a Dropdownlist.

public static List<Uf> GetAll()
{
  var client = new WebClient();
  JsonSerializerSettings settings = new JsonSerializerSettings();
  settings.Culture = new System.Globalization.CultureInfo("pt-BR");
  var response = client.DownloadString(new Uri(HttpContext.Current.Server.MapPath("~/Scripts/uf.json")));
  var lista = JsonConvert.DeserializeObject<List<Uf>>(response, settings);
  return lista;
}

But when it returns this to the Browser, it displays the accented characters with strange codes.

Texto com caracteres extranhos

How can I display characters with correct accentuation?

1 answer

6


Perhaps the problem has nothing to do with the disembolarization but with the download in itself.

Define the encoding in the request should solve your problem

var client = new WebClient();
client.Encoding = Encoding.UTF8;

var response = client.DownloadString(new Uri(HttpContext.Current.Server.MapPath("~/Scripts/uf.json")));
  • I was trying to change the encoding at the time of presenting the data but not at the time of downloading. It worked. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.