【问题标题】:c# json array response from http get garbledc#来自http的json数组响应出现乱码
【发布时间】:2013-11-18 09:15:30
【问题描述】:

我正在尝试从 API 获取信息,该 API 以 JSON 数组形式返回结果。

这是我的代码:

string sURL = "http://api.planets.nu/games/list?limit=1";
WebRequest wrGetURL = WebRequest.Create(sURL);
StreamReader objReader = new StreamReader(wrGetURL.GetResponse().GetResponseStream());
string sLine;
sLine = objReader.ReadToEnd();
Console.WriteLine(sLine);

在 Firefox 中粘贴 URL 时,我得到的是纯文本:

[{"name":"Upikarium Sector","description":"This is a battle for the Upikarium Sector. This is a default configuration game. Custom map. This game has 2 turns per week.","shortdescription":"","status":2,"datecreated":"8\/28\/2011 10:11:28 PM","dateended":"1\/1\/0001 12:00:00 AM","maptype":2,"gametype":2,"wincondition":1,"difficulty":1.07299030764822,"tutorialid":0,"requiredlevelid":0,"maxlevelid":0,"masterplanetid":467,"quadrant":23,"mintenacity":0,"faststart":0,"turnsperweek":2,"yearstarted":8,"isprivate":false,"scenarioid":0,"createdby":"none","turn":229,"slots":11,"turnstatus":"xx3x_67x___","hostdays":"_M___F_","slowhostdays":"","hosttime":"18:3","lastbackuppath":"c:\\planetsdata\\backups\\game22158\\turn228-635191849885449557.zip","nexthost":"11\/8\/2013 6:03:00 PM","allturnsin":false,"lastnotified":false,"ishosting":false,"lastloadeddate":"11\/6\/2013 9:16:22 PM","deletedate":"","lasthostdate":"11\/4\/2013 6:04:49 PM","password":"","haspassword":false,"statusname":"Running","timetohost":"Next turn in 44 hours","id":22158}]

这是我应该用我的代码得到的。一个数组,带有一个保存游戏信息的对象。

但是,writeline 的结果是一堆乱码。我似乎无法获得正确的文本结果。我已经为此烦恼了一段时间,不知道为什么。这可能与返回的是 JSON 数组有关,因为对返回 JSON 字典的 API 的 get 调用会产生完全可读的文本。但是,这拒绝成为可读字符串!

我很确定我错过了一些简单的东西,但它不会影响到我!

【问题讨论】:

  • 你试过用json解析吗?
  • 我不需要得到可读的结果来解析吗?我得到的是: ▼ ??zR ?]SMo?0♀?+??Yj?c?6♀?ð??Jv?e?↕@O? ??#???pDY??‼=???(?b?←?kM??f ·???♥?☺?▼Q?? ????r+??♫{?G/Z? {z?-?d?KnN?ys¶G?? ?☺?H???vbB+N?O[▬?♠c????z?u?j?ck??;~+??4 ?↕??Jj?? ¶????C?E?j??$I¤?N↕>?} ?(⌂?0&♫??.'??KG?♦????▲/| ?&????,?↨y?2???H\-?H??3Y?F|?W???h?◄4???~#?g?,h?↕2?? ????P?Cz♫a#???h↔;↨???6>??_$7Yza↨d???]l9?%?JX]|??R←?rI????? ?Rk?u☻?|??b⌂>∟♫∟0↑?;????=C▼♥??⌂▲V??????3FFn?☺?y??☼??u? ,v8▲↔4???&♀&M?l??$M?wE?S?*?,wy?Ru??X?XkT?*?u?pD?va??.????Wmy? WK?♦???V??]??!?A?_?Y??g??z????]?? -?\♀f??Ia????? ?☻??? ?↕♦
  • 根据该评论,我对错误的副本投了赞成票。这就是你要找的东西:HttpWebRequest & Native GZip Compression.

标签: c# json


【解决方案1】:

正如文档中所述,内容已压缩,添加对 System.IO.Compression 的引用并使用以下代码:

 string sURL = "http://api.planets.nu/games/list?limit=1";
 HttpWebRequest wrGetURL = (HttpWebRequest)WebRequest.Create(sURL);
 wrGetURL.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
 StreamReader objReader = new StreamReader(wrGetURL.GetResponse().GetResponseStream(), Encoding.UTF8);
 string sLine;
 sLine = objReader.ReadToEnd();
 Console.WriteLine(sLine);

【讨论】:

  • 啊!我知道我忽略了一些东西,非常感谢。这实际上是有道理的......
  • 不知道该属性。不过,它是在 HttpWebRequest 中定义的。更新了答案,因为这是一种更好的方法。另一个注意事项;您可能不想编写自己的 JSON 解析器。
【解决方案2】:

这是压缩内容。解决这个问题的最简单方法是添加

wrGetURL.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

在您收到回复之前。

希望这会有所帮助,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-24
    • 2012-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 2021-01-08
    相关资源
    最近更新 更多