0

Good evening,

I'm downloading a .JSON file from a CDN which gives me a 12kb file. I've opened this file (export.json) and verified that all looks correct from an encoding standpoint and is standard JSON syntax.

I use the following code to download it to my server so I can parse it:

 //webclient downloads the JSON file
    var webClient = new WebClient ();
    webClient.Encoding = System.Text.Encoding.UTF8;
    string result = string.Empty;
    try
    {
        result = webClient.DownloadString("somewebsite/export.json");
        File.WriteAllText(@"C:\Users\WebDev\Documents\Visual Studio 2015\WebSites\test\json.json", result);
    }
    catch (Exception ex) {
        System.Diagnostics.Debug.Write(ex);
    }

        //parses downloaded file    

        Newtonsoft.Json.Linq.JObject o = Newtonsoft.Json.Linq.JObject.Parse(File.ReadAllText(@"C:\Users\WebDev\Documents\Visual Studio 2015\WebSites\test\json.json"));
        //creates a dictionary at the outer object level (key=id and value=inner object
        var dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(o.ToString());

I've also tried using the DownloadFile method. Each time, I get something like this in the .JSON file I generate "�b��X export.json.tmp �\�R�H�~���:��)u�b���@"

Any ideas what I'm missing? Thanks.

  • Try [this solution](http://stackoverflow.com/a/30049848/3744182) that gets the encoding from the response headers. – dbc Mar 06 '17 at 02:38

1 Answers1

0

Use httpclient, httpclient auto analysis response header.

彭 伟
  • 49
  • 3