无法解析StackExchange API响应 [英] Unparseable StackExchange API response

查看:106
本文介绍了无法解析StackExchange API响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个小程序,从StackExchange API分析我的个人资料数据,但API返回unparse- /读取数据给我。

I've written a small program to analyze my profile data from the StackExchange API, but the api returns unparse-/unreadable data to me.

收到的数据:(使用C#自下载)

\\\\b\0\0\ 0\0\0\\\\0mRMo0\f /:D $ C'^ {/ \\\\\\G> \I\ u0015\\\݀D> GRL'o\\\G%JP\\\-EM> 0Xbm 〜\\\tk\\\M] rdLGv0〜FJ = 1\\\1I> kTRA\(/ +; Nl\\\ 3 H \\\P藄XaLw#3\\\ + \\\\\\\\\p】v \\\\tڧ\NF \"\\\\\\eƺ_1x#J(1)-c AX\t\\\T @ qj\\\U7\\\\\a ^ \b#\\\QG%y\tחq00K\\ \\av\\\ {ظ\\\\\\\ + | \\\'\\\〜8\\\\\\-H] O\\\V o\\\\\\〜Y\\\\\\\0\0

\u001f�\b\0\0\0\0\0\u0004\0mRMo�0\f�/:�d$�c˱�'�^{/\u0006��\u0018G�>\I��\u0015���\u0004݀�D>�GR�L'���o��\u0004�G���%JP\u001c����-��Em>0���X�bm~�\u0018tk��\u0014�M]r�dLG�v0~Fj=���1\u00031I�>kTRA\"(/+.����;Nl\u0018�?h�\u0014��P藄�X�aL��w���#�3\u0002��+�\u007f���\u0010���\u000f�p�]��v\u007f���\t��ڧ�\nf��״\u0018\u0014eƺ�_��1x#j^-�c� � AX\t���\u001aT��@qj\u001aU7�����\u0014\"\a^\b�#\u001e��QG��%�y�\t�ח������q00K\av\u0011{ظ���\u0005\"\u001d+|\u007f���'�\u0016~��8\u007f�\u0001-h�]O\u007fV�o\u007f\u0001~Y\u0003��\u0002\0\0

Data希望:(从我的浏览器复制粘贴)

Data wanted: (copy-pasted from my browser)

{项目:[{badge_counts,{青铜:987,银子:654,金:321},ACCOUNT_ID123456789,is_employee:假的,LAST_MODIFIED_DATE:1250612752,last_access_date:1250540770,时代:0,reputation_change_year :987,reputation_change_quarter:654,reputation_change_month:321,reputation_change_week:98,reputation_change_day:76,信誉:9876,CREATION_DATE:1109670518,USER_TYPE:注册,USER_ID 123456789,accept_rate:0,位置:澳大利亚,WEBSITE_URL: http://example.org 链接: http://example.org/username ,profile_image:的http://example.org/username/icon.png ,DISPLAY_NAME:用户名}],has_more:假的,quota_max: 300,quota_remaining:300}

{"items":[{"badge_counts",{"bronze":987,"silver":654,"gold":321},"account_id":123456789,"is_employee":false,"last_modified_date":1250612752,"last_access_date":1250540770,"age":0,"reputation_change_year":987,"reputation_change_quarter":654,"reputation_change_month":321,"reputation_change_week":98,"reputation_change_day":76,"reputation":9876,"creation_date":1109670518,"user_type":"registered","user_id":123456789,"accept_rate":0,"location":"Australia","website_url":"http://example.org","link":"http://example.org/username","profile_image":"http://example.org/username/icon.png","display_name":"username"}],"has_more":false,"quota_max":300,"quota_remaining":300}

我写这个(扩展)方法来从网上下载的字符串:

I've written this (extension) method to download a string from the internet:

public static string DownloadString(this string link)
{
    WebClient wc = null;
    string s = null;
    try
    {
        wc = new WebClient();
        wc.Encoding = Encoding.UTF8;
        s = wc.DownloadString(link);
        return s;
    }
    catch (Exception)
    {
        throw;
    }
    finally
    {
        if (wc != null)
        {
            wc.Dispose();
        }
    }
    return null;
}



我再在网上搜索,发现下载方法的字符串,用其他一些战术:

I've then searched the internet and found a method for downloading strings, using some other tactics:

public string DownloadString2(string link)
{
    WebClient client = new WebClient();
    client.Encoding = Encoding.UTF8;
    Stream data = client.OpenRead(link);
    StreamReader reader = new StreamReader(data);
    string s = reader.ReadToEnd();
    data.Close();
    reader.Close();
    return s;
}



不过,这两种方法返回相同的(unread- /不可解析)的数据。

But both methods return the same (unread-/unparseable) data.

我如何可从API获取可读数据?有没有遗漏什么吗?

How can I get readable data from the API? Is there anything missing?

推荐答案

在我看来,输出被压缩。您可以使用 GZipStream 可在 System.IO.Compression ,以解压缩字节被发现。

It seems to me that the output is compressed. You could use the GZipStream which can be found in System.IO.Compression in order to decompress the bytes.

public static string DownloadString(this string link)
{
    WebClient wc = null;
    try
    {
        wc = new WebClient();
        wc.Encoding = Encoding.UTF8;
        byte[] b = wc.DownloadData(link);

        MemoryStream output = new MemoryStream();
        using (GZipStream g = new GZipStream(new MemoryStream(b), CompressionMode.Decompress))
        {
            g.CopyTo(output);
        }

        return Encoding.UTF8.GetString(output.ToArray());
    }
    catch
    {

    }
    finally
    {
        if (wc != null)
        {
            wc.Dispose();
        }
    }
    return null;
}

这篇关于无法解析StackExchange API响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆