2014-09-04 46 views
1

我想獲得問題的總數問題,我正在做一個練習,並堅持在反序列化的stackoverflow。我在哪裏出錯反序列化

如果有人能請讓我知道我需要做些什麼,或者提供一些能幫助我的示例代碼,我將不勝感激。

,當我在控制檯中運行這件事我得到錯誤 「有反序列化類型。遇到意外的字符的對象的錯誤」,這是一個向下的三角形...

這就是我到目前爲止

using System; 
using System.Threading.Tasks; 
using System.Net; 
using System.Net.Http; 
using System.Net.Http.Headers; 
using System.Web; 
using System.IO; 
using System.Runtime.Serialization; 
using System.Runtime.Serialization.Json; 


namespace cs_StackOverflowAPI 
{ 
    [DataContract(Namespace = "http://schemas.microsoft.com/search/local/ws/rest/v1")] 
    public class Question 
    { 
     [DataMember(Name = "question_id")] 
     public int Questions { get; set; } 

    } 

    [DataContract] 
    public class ResourceSet 
    { 
     [DataMember(Name = "estimatedTotal")] 

     public long EstimatedTotal { get; set; } 
     [DataMember(Name = "resources")] 
     public Question[] Resources { get; set; } 
    } 

    [DataContract] 
    public class Response 
    { 

     [DataMember(Name = "statusCode")] 
     public int StatusCode { get; set; } 
     [DataMember(Name = "statusDescription")] 
     public string StatusDescription { get; set; } 
     [DataMember(Name = "authenticationResultCode")] 
     public string AuthenticationResultCode { get; set; } 
     [DataMember(Name = "errorDetails")] 
     public string[] errorDetails { get; set; } 
     [DataMember(Name = "traceId")] 
     public string TraceId { get; set; } 
     [DataMember(Name = "resourceSets")] 
     public ResourceSet[] ResourceSets { get; set; } 
    } 



    class Program 
    { 
     public string serverUrl = "https://api.stackexchange.com/"; 
     public HttpClient client = null; 

     static void Main(string[] args) 
     { 
      try 
      { 
       string noOfQuestions = CreateRequest("questions"); 
       Response noOfQuestionsReponse = MakeRequest(noOfQuestions); 
       ProcessResponse(noOfQuestionsReponse); 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine(e.Message); 
       Console.Read(); 
      } 

     } 


     public static string CreateRequest(string queryString) 
     { 
     string UrlRequest = "https://api.stackexchange.com/2.2/" + 
           queryString + 
           "?&site=stackoverflow"; 
      return (UrlRequest); 
     } 

     public static Response MakeRequest(string requestUrl) 
     { 
      try 
      { 
       HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest; 
       using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) 
       { 
        if (response.StatusCode != HttpStatusCode.OK) 
         throw new Exception(String.Format(
         "Server error (HTTP {0}: {1}).", 
         response.StatusCode, 
         response.StatusDescription)); 
        DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Response)); 
        object objResponse = jsonSerializer.ReadObject(response.GetResponseStream()); 
        Response jsonResponse 
        = objResponse as Response; 
        return jsonResponse; 
       } 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine(e.Message); 
       return null; 
      } 
     } 

     static public void ProcessResponse(Response noOfQuestionsReponse) 
     { 

      // this is where the error occurs 
      Console.WriteLine("Press any key to exit"); 
      Console.ReadKey(); 
     } 


     public static long ToUnixTime(DateTime date) 
     { 
      var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); 
      return Convert.ToInt64((date.ToUniversalTime() - epoch).TotalSeconds); 
     } 
    } 
} 
+0

您是否考慮過簡單地使用StacMan或其他現有庫? – 2014-09-04 10:49:26

+0

我不知道SO api,也沒有遇到「向下指向的三角形...」,但我建議你使用NewtonSoft.Json庫進行json de/serializations。 – 2014-09-04 10:50:03

+0

https://api.stackexchange.com/2.2/questions?&site=Stackoverflow 此網址的迴應與回覆類 – Patel 2014-09-04 11:16:13

回答

1

它返回壓縮內容(gzip,具體而言)。如果可能的話,告訴你的http客戶端自動解壓縮它:

request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate"); 
request.AutomaticDecompression = 
    DecompressionMethods.GZip | DecompressionMethods.Deflate; 

下一個問題是:架構不匹配;問題在items的根目錄中:

[DataContract] 
public class Response 
{ 
    [DataMember(Name ="items")] 
    List<Question> questions { get;set; } 
} 
+0

的屬性不匹配,您會在哪裏建議這樣做?我對這類要求的經驗非常有限。 – 2014-09-04 11:02:31

+0

@SimonPrice立即的GetResponse()' – 2014-09-04 11:03:01

+0

@SimonPrice,再次之前',在這種情況下,我強烈建議您使用與變化像StacMan – 2014-09-04 11:03:16