c# - Cannot decode JSON returned by HttpWebRequest -
i trying decode json returned website http://wthrcdn.etouch.cn/weather_mini?city=北京
. when @ url firefox using unicode encoding, see following valid json:
{"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":"北京"}}
but if try , decode json httpwebrequest
result gibberish:
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);
produces
"�\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"
decoding encoding.unicode
produces:
"謟\b\0\0\0鎭䫝䄂윔╟⋕ꅟẋ먠琘섵�ጕⰑ௭迌됰䰴䩒닊늈橝홦㨕邃瘭픡컝�칯워䞑�藈එ㩩⏖爚◙뉉༡㇖앲呑稍ဢ░灰蜅䕠槳罞鵢캶鏀諸鳱릡如놬赴Ὦꪰ晡ٷ⦯鋲佽鉮췁�폨維�핧洚覤㖑왛鎠뭢幽➺㛝ℴ⚽�鬍蟀슆᮪㩫釆ൗ훺㣽Ϩ嘕攺ힳ柦驙蠯밨㗫䴔᳣쮊�鐲旸疙卛㩛唅똽說䜞냷萈ꨌ愢〓鄇橖犲㡍⩗愨䪳递뎾榕俇䕠育ᢘㆅ賶涅䋿ꌓ踸䳎隈嚝❴睇〻幮ᢹ瀓ј퇤럞揚뼂;ⓧ끳⡪责㒟♦僑ᨬ톎瑱⢓撱ㅃ퍕냕윇࿄ᅼ쎅迺罪趼�蜷ᠽ숅扫皣᳇�㵾밶쐘䊸쒉G퐃"
how can decode json web site using httpwebrequest
?
the response compressed need enable automatic decompression. also, should wrap disposables in using
statements:
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 json string. // starts out {"desc":"ok","status":1000,"data":{"wendu":"20","ganmao":"各项气象条件适宜,... console.writeline(jsonstr); }
note console doesn't display chinese characters. if yours doesn't either, result of writing console contain many "?" characters.
Comments
Post a Comment