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