0
晚上好,WebClient编码问题
我正在从一个CDN中下载一个.JSON文件,它给了我一个12kb的文件。我已经打开了这个文件(export.json),并且从编码的角度证明了所有的看起来都是正确的,并且是标准的JSON语法。
我使用下面的代码将其下载到我的服务器,所以我可以解析它:
//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());
我使用DownloadFile方法也试过。每次我在.JSON文件中得到类似这样的东西:“ b Xexport.json.tmp \ R H 〜 : )u b @”
任何想法我失踪?谢谢。
请尝试[此解决方案](http://stackoverflow.com/a/30049848/3744182)从响应头获取编码。 – dbc