我想在 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,���...
?