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

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -