Problems with accentuation when reading JSON file data

4

The method below is extracting data from a JSON file, to populate 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 you return this to the browser, it displays accented characters with foreign codes.

How can I display the correct accented characters?

    
asked by anonymous 10.10.2016 / 21:30

1 answer

5

Possibly the problem does not have to do with deserialization, but with the download .

Defining 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")));
    
10.10.2016 / 21:35