无法解码HttpWebRequest返回的JSON

时间:2015-04-29 10:14:05

标签: c# json http httpwebrequest

我正在尝试解码网站sparkConf.set("spark.executor.instances", "6") 返回的JSON。当我使用Unicode编码使用Firefox查看此URL时,我看到以下有效的JSON:

http://wthrcdn.etouch.cn/weather_mini?city=北京

但是,如果我尝试使用{"desc":"OK","status":1000,"data":{"wendu":"20","ganmao":"各项气象条件适宜,发生感冒机率较低。但请避免长期处于空调房间中,以防感冒。","forecast":[{"fengxiang":"无持续风向","fengli":"微风级","high":"高温 27℃","type":"晴","low":"低温 15℃","date":"30日星期四"},{"fengxiang":"南风","fengli":"3-4级","high":"高温 30℃","type":"多云","low":"低温 16℃","date":"1日星期五"},{"fengxiang":"北风","fengli":"3-4级","high":"高温 25℃","type":"小到中雨","low":"低温 14℃","date":"2日星期六"},{"fengxiang":"无持续风向","fengli":"微风级","high":"高温 25℃","type":"多云","low":"低温 14℃","date":"3日星期天"},{"fengxiang":"无持续风向","fengli":"微风级","high":"高温 26℃","type":"多云","low":"低温 13℃","date":"4日星期一"}],"yesterday":{"fl":"微风","fx":"无持续风向","high":"高温 27℃","type":"晴","low":"低温 15℃","date":"29日星期三"},"aqi":"149","city":"北京"}} 获取并解码此JSON,结果总是胡言乱语:

HttpWebRequest

可生产

string http = "http://wthrcdn.etouch.cn/weather_mini?city=北京";

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(http);    //创建一个请求示例
HttpWebResponse response = (HttpWebResponse)request.GetResponse();  //获取响应,即发送请求
Stream responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);

string jsonStr = streamReader.ReadToEnd();
Console.WriteLine(jsonStr);

使用"�\b\0\0\0\0\0\0\0���JA�_%��`��\"_�� �t5���,�\v̏0�4LRJʲ����]jfֹ�:��-v!�������o��ƑG��ȅ�\ri:�#r�%I�!�1r�QT\rz\"�%��pp�`E�i^b����N��������Y��t�n��afw�)�}On�����#��ӭ}~�g�m���5[Ơ�b�}^�'�64!�&/�\r������ªk:r-ƑW\r���8�V:e�\r���gY�/�(�\r����5M���~�2��e�u[S��[:U=���G��\b���\f�\"aw0\a�P$Vj�rM8W*(a�J������i�O`E��i��1���m�B�8��L���Vt'Gw;0n^�pX��޷�c�;\0�$s�j(#�F�4f&�P,��qt�(�dC1U�հ\a���\r|����j����7�=�kb�v���~=6�ĸB��'��L�\0\0" 进行解码会产生:

Encoding.Unicode

如何使用"謟\b\0\0\0鎭䫝䄂윔╟⋕ꅟẋ먠琘섵�ጕⰑ௭迌됰䰴䩒닊늈橝홦㨕邃瘭픡컝￙�칯워䞑�藈එ㩩⏖爚◙뉉༡㇖앲呑稍ဢ░灰蜅䕠槳罞鵢캶鏀諸鳱릡如놬赴Ὦꪰ晡ٷ⦯鋲佽鉮췁�폨維�핧洚覤㖑왛鎠뭢幽➺㛝ℴ⚽�鬍蟀슆᮪㩫⵲釆ൗ훺㣽Ϩ嘕攺඙ힳ柦驙蠯밨㗫䴔᳣쮊�鐲旸疙卛㩛唅똽說䜞냷萈ꨌ愢๷〓鄇⑐橖犲㡍⩗愨䪳﷐递뎾榕俇䕠育ᢘㆅ賶涅䋿ꌓ踸䳎隈嚝❴睇〻幮ᢹ瀓ј퇤럞揚뼂;ⓧ끳⡪责㒟♦僑ᨬ톎瑱⢓撱ㅃ퍕냕윇࿄෣ᅼ쎅迺罪趼�蜷ᠽ숅扫皣᳇�㵾밶쐘䊸쒉G퐃" 解析此网站的JSON?

1 个答案:

答案 0 :(得分:1)

响应已压缩,因此您需要启用automatic decompression。此外,您应该将您的一次性用品包裹在using陈述中:

        string http = "http://wthrcdn.etouch.cn/weather_mini?city=北京";

        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(http);    //创建一个请求示例
        request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
        using (var response = (HttpWebResponse)request.GetResponse())  //获取响应,即发送请求
        using (var responseStream = response.GetResponseStream())
        using (var streamReader = new StreamReader(responseStream, Encoding.UTF8))

        {
            string jsonStr = streamReader.ReadToEnd();
            // Use the JSON string.
            // Starts out with {"desc":"OK","status":1000,"data":{"wendu":"20","ganmao":"各项气象条件适宜,...
            Console.WriteLine(jsonStr);
        }

请注意,我的控制台不显示中文字符。如果你没有,那么写入控制台的结果将包含许多"?"字符。