2012-11-17 42 views
6



Mã của tôi hoạt động tốt khi gọi URL REST:
http://api.feedzilla.com/v1/categories.jsonASP.NET đúc web API http đối phó với mảng json

nhưng khi tôi gọi sau URL tôi nhận được lỗi:
http://api.feedzilla.com/v1/categories/15/articles.json?count=36&since=2012-11-15&client_source=&order=relevance&title_only=0&

Lỗi:

{"Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'System.Collections.Generic.IEnumerable`1[Nitin.News.DAL.Resources.Article]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.\r\nPath 'articles', line 1, position 12."} 

Mã của tôi như sau:

public class Article 
    { 
     public string publish_date { get; set; } 
     public string source { get; set; } 
     public string source_url { get; set; } 
     public string summary { get; set; } 
     public string title { get; set; } 
     public string url { get; set; } 
    } 
    public IEnumerable<Article> Get() 
    { 
     HttpClient client = new HttpClient(); 
     client.BaseAddress = new Uri("http://api.feedzilla.com/v1/"); 

     //Add an Accept header for JSON format. 
     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

     // call the REST method 
     HttpResponseMessage response = client.GetAsync("http://api.feedzilla.com/v1/categories/2/articles.json??count=36&since=2012-11-15&client_source=&order=relevance&title_only=0&").Result; // Blocking call! 
     if (response.IsSuccessStatusCode) 
     { 
      // Parse the response body. Blocking! 
      return response.Content.ReadAsAsync<IEnumerable<Article>>().Result; 

      //wont work 
      //string JSON =response.Content.ReadAsStringAsync().Result; 
      //return JsonConvert.DeserializeObject<IEnumerable<T>>(JSON); 
     } 
     else 
     { 
      throw new Exception(string.Format("Data access faild,{0} ({1}) method:{2}", (int)response.StatusCode, response.ReasonPhrase, MethodURL)); 
     } 
    } 

Trả lời

14

Bạn cần một cấp độ khác trong đối tượng hierachy của bạn ... tức là thư mục gốc chứa IEnumerable.

runing JSON vào công cụ này tại http://json2csharp.com/ tạo ra các lớp proxy sau:

public class Enclosure 
{ 
    public int length { get; set; } 
    public string media_type { get; set; } 
    public string uri { get; set; } 
} 

public class Article 
{ 
    public string author { get; set; } 
    public string publish_date { get; set; } 
    public string source { get; set; } 
    public string source_url { get; set; } 
    public string summary { get; set; } 
    public string title { get; set; } 
    public string url { get; set; } 
    public List<Enclosure> enclosures { get; set; } 
} 

public class RootObject 
{ 
    public List<Article> articles { get; set; } 
    public string description { get; set; } 
    public string syndication_url { get; set; } 
    public string title { get; set; } 
} 

Bạn chỉ cần thay đổi mã của bạn để điều này thì:

// Parse the response body. Blocking! 
return response.Content.ReadAsAsync<RootObject>().Result.articles; 

Rõ ràng loại bỏ bất kỳ thuộc tính bạn không nhu cầu. Chỉ nghĩ rằng tôi sẽ cho họ thấy tất cả những điều đáng quan tâm.

+0

Cảm ơn bạn đã trả lời, nhưng tiếc là tôi nhận được thông báo lỗi tương tự. –

+0

@NitinJS xin lỗi khi nghe điều đó. Tôi đã thử nghiệm mã và đi từ lỗi của bạn để giải quyết nó. Tôi có thể tải lên toàn bộ dự án làm việc nếu bạn thích? Bạn đã thay đổi dòng mã này chưa? esponse.Content.ReadAsAsync ()? –

+0

Xin chào, Cảm ơn, mã của bạn đã hoạt động: D đã thay đổi kiểu trả về từ 'IEnumerable 'thành' T' –

Các vấn đề liên quan