3

我想在 WinRT 中反序列化 JSON,例如我正在尝试 StackExchange Api 2.1。

我有这样的方法:

public static async Task<string> GetJsonString()
        {
            HttpClient client = new HttpClient();
            string url = @"https://api.stackexchange.com/2.1/answers?fromdate=1349913600&order=desc&min=20&sort=votes&site=stackoverflow";
            HttpResponseMessage response = await client.GetAsync(url);
            return await response.Content.ReadAsStringAsync();
        }

        public static async void ParseJson()
        {
            string responseText = await GetJsonString();

            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(JsonResponse));
            JsonResponse root;
            using (MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(responseText)))
            {
                root = serializer.ReadObject(stream) as JsonResponse;
            }
        }

和这些类:

[DataContract]
class Answer
{
    [DataMember(Name = "question_id")]
    public int QuestionId { get; set; }

    [DataMember(Name = "answer_id")]
    public int AnswerId { get; set; }

    [DataMember(Name = "creation_date")]
    public int CreationDate { get; set; }

    [DataMember(Name = "last_edit_date")]
    public int LastEditDate { get; set; }

    [DataMember(Name = "last_activity_date")]
    public int LastActivityDate { get; set; }

    [DataMember(Name = "score")]
    public int Score { get; set; }

    [DataMember(Name = "community_owned_date")]
    public int CommunityOwnedDate { get; set; }

    [DataMember(Name = "is_accepted")]
    public bool IsAccepted { get; set; }

    [DataMember(Name = "owner")]
    public Owner Owner { get; set; }
}

[DataContract]
class Owner
{
    [DataMember(Name = "user_id")]
    public int UserId { get; set; }

    [DataMember(Name = "display_name")]
    public string DisplayName { get; set; }

    [DataMember(Name = "reputation")]
    public int Reputation { get; set; }

    [DataMember(Name = "user_type")]
    public string UserType { get; set; }

    [DataMember(Name = "profile_image")]
    public string ProfileImage { get; set; }

    [DataMember(Name = "link")]
    public string Link { get; set; }

    [DataMember(Name = "accept_rate")]
    public int? AcceptRate { get; set; }
}

[DataContract]
class JsonResponse
{
    [DataMember(Name = "items")]
    public IList<Answer> Answers { get; set; }

    [DataMember(Name = "quota_remaining")]
    public int QuotaRemaining { get; set; }

    [DataMember(Name = "quota_max")]
    public int QuotaMax { get; set; }

    [DataMember(Name = "has_more")]
    public bool HasMore { get; set; }
}

我做错了什么,在 responseText 我得到这样的字符:\b\0\0\0\0\0\0�ێ���_E�E�FfwW\r,���...

4

1 回答 1

5

您的 url 似乎返回了压缩数据,而 HttpClient 不知何故不理解这一点。下面的代码解决了这个问题:

public static async Task<string> GetJsonString()
{
    HttpClient client = new HttpClient();
    string url = @"https://api.stackexchange.com/2.1/answers?fromdate=1349913600&order=desc&min=20&sort=votes&site=stackoverflow";
    HttpResponseMessage response = await client.GetAsync(url);

    byte[] buf = await response.Content.ReadAsByteArrayAsync();
    GZipStream zipStream = new GZipStream(new MemoryStream(buf), CompressionMode.Decompress);
    StreamReader reader = new StreamReader(zipStream);
    return reader.ReadToEnd();
}
于 2012-10-26T10:07:23.180 回答