C# 网络客户端编码问题
晚上好, 我正在从一个CDN下载一个.JSON文件,它给了我一个12kb的文件。我已经打开了这个文件(export.json),并验证了从编码的角度来看,所有这些都是正确的,并且是标准的json语法 我使用以下代码将其下载到服务器,以便对其进行解析:C# 网络客户端编码问题,c#,.net,json,json.net,C#,.net,Json,Json.net,晚上好, 我正在从一个CDN下载一个.JSON文件,它给了我一个12kb的文件。我已经打开了这个文件(export.json),并验证了从编码的角度来看,所有这些都是正确的,并且是标准的json语法 我使用以下代码将其下载到服务器,以便对其进行解析: //webclient downloads the JSON file var webClient = new WebClient (); webClient.Encoding = System.Text.Encoding.UTF
//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());
//webclient下载JSON文件
var webClient=newwebclient();
webClient.Encoding=System.Text.Encoding.UTF8;
字符串结果=string.Empty;
尝试
{
结果=webClient.DownloadString(“somewebsite/export.json”);
writealText(@“C:\Users\WebDev\Documents\Visual Studio 2015\WebSites\test\json.json”,result);
}
捕获(例外情况除外){
系统.诊断.调试.写入(ex);
}
//解析下载的文件
Newtonsoft.Json.Linq.JObject o=Newtonsoft.Json.Linq.JObject.Parse(File.ReadAllText(@“C:\Users\WebDev\Documents\visualstudio 2015\WebSites\test\Json.Json”);
//在外部对象级别创建字典(key=id,value=internalobject
var dict=JsonConvert.DeserializeObject(o.ToString());
我也尝试过使用DownloadFile方法。每次,我在生成的.JSON文件中都会得到类似的内容“�B��X export.json.tmp�\�R�H�~���:��)U�B���@"
知道我缺少什么吗?谢谢。使用httpclient,httpclient自动分析响应头。尝试从响应头获取编码。