2015-12-21 143 views
2

我有我自己的代碼在這裏,已經得到了JSON結果,然後我不知道將JSON結果移動到下面的接口(IEntities函數)列表中。如何獲得JSON結果到列表

class GetCategory : IEntities 
{ 
    private JsonHandle _jsonhandle; 
    private string _ocategory; 

    public async void TaskCategory() 
    { 
     _jsonhandle = new JsonHandle(); 
     _jsonhandle.StrAPI = "http://api.nytimes.com/svc/books/v3/lists/names.json?api-key=7bb034b7693d6f9753b2f68e00b98c78%3A16%3A73599437"; 
     var client = new HttpClient(); 
     Task<string> datatask = client.GetStringAsync(_jsonhandle.StrAPI); 
     try 
     { 
      var JsonRead = await datatask; 
      JObject oCategory = JObject.Parse(JsonRead); 
      List<JToken> results = oCategory["results"].Children().ToList(); 

      //serialize JSON results into .NET objects 
      List<object> dtCategory = new List<object>(); 
      foreach (JToken result in results) 
      { 
       object _dtcategory = JsonConvert.DeserializeObject<object>(result.ToString()); 
       dtCategory.Add(_dtcategory); 
       var listname = result["list_name"]; 
      } 
     } 
     catch (Exception error) 
     { 
      Console.WriteLine("AW!" + error.StackTrace); 
     } 

    public List<object> BookCategory() 
    { 

    } 
} 

在在IEntities接口,我需要把我的JSON結果在接口List<object>的最後一個函數。

+0

'名單'不是接口... –

回答

3

JSON工作時,首先要做的是創建一個模型對象。爲此,您應該手動分析JSON輸出,或者您可以通過轉到以下鏈接並粘貼您要使用的JSON或服務鏈接自動生成模型;

json2csharp.com

我只是用你的API鏈接,併產生輸出;

public class Result 
{ 
    public string list_name { get; set; } 
    public string display_name { get; set; } 
    public string list_name_encoded { get; set; } 
    public string oldest_published_date { get; set; } 
    public string newest_published_date { get; set; } 
    public string updated { get; set; } 
} 

public class RootObject 
{ 
    public string status { get; set; } 
    public string copyright { get; set; } 
    public int num_results { get; set; } 
    public List<Result> results { get; set; } 
} 

這將是我們的模型。

其次,據我所看到的,你想要做的是隻拿到結果列表中,因此,當你反序列化JSON 輸出,你要使用Model.Result和移動他們變成了列表

然後,獲得響應,可以使用私有方法,它返回一個async Task<string>,並在BookCategory()方法,您可以根據您的JSON對象模型得到的結果和反序列化JSON並初始化列表;

public List<Model.Result> BookCategory() 
{ 
    List<Model.Result> list = new List<Model.Result>(); 

    var model = JsonConvert.DeserializeObject<Model.RootObject>(TaskCategory().Result); 

    list = model.results; 

    return list; 
} 

反序列化JSON是簡單,如下面;

var model = JsonConvert.DeserializeObject<Model.RootObject>(TaskCategory().Result); 

Model.cs
using System.Collections.Generic; 

namespace SO1 
{ 
    public class Model 
    { 
     public class Result 
     { 
      public string list_name { get; set; } 
      public string display_name { get; set; } 
      public string list_name_encoded { get; set; } 
      public string oldest_published_date { get; set; } 
      public string newest_published_date { get; set; } 
      public string updated { get; set; } 
     } 

     public class RootObject 
     { 
      public string status { get; set; } 
      public string copyright { get; set; } 
      public int num_results { get; set; } 
      public List<Result> results { get; set; } 
     } 
    } 
} 

GetCategory.cs
using Newtonsoft.Json; 
using System.Collections.Generic; 
using System.Threading.Tasks; 
using System.Net.Http; 
using System; 

namespace SO1 
{ 
    public class GetCategory : IEntities 
    { 
     private String BaseUri; 

     public GetCategory(string BaseUri) 
     { 
      this.BaseUri = BaseUri; 
     } 

     private async Task<string> TaskCategory() 
     { 
      var httpClient = new HttpClient(); 

      var parameters = new Dictionary<string, string>(); 
      parameters["text"] = "text"; 

      var response = await httpClient.GetStringAsync(BaseUri); 

      return response;   
     } 

     public List<Model.Result> BookCategory() 
     { 
      List<Model.Result> list = new List<Model.Result>(); 

      var model = JsonConvert.DeserializeObject<Model.RootObject>(TaskCategory().Result); 

      list = model.results; 

      return list; 
     } 

    } 
} 

IEntities
using System.Collections.Generic; 

namespace SO1 
{ 
    public interface IEntities 
    { 
     List<Model.Result> BookCategory(); 
    } 
} 

的Program.cs
using System; 
using System.Collections.Generic; 

namespace SO1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string BaseUri = "http://api.nytimes.com/svc/books/v3/lists/names.json?api-key=7bb034b7693d6f9753b2f68e00b98c78%3A16%3A73599437"; 

      IEntities entity = new GetCategory(BaseUri); 

      List<Model.Result> listBookCategory = new List<Model.Result>(); 

      listBookCategory = entity.BookCategory(); 

      foreach (Model.Result r in listBookCategory) 
      { 
       Console.WriteLine(); 
       Console.WriteLine("...List Name    : " + r.list_name); 
       Console.WriteLine("...Display Name   : " + r.display_name); 
       Console.WriteLine("...List Name Encoded  : " + r.list_name_encoded); 
       Console.WriteLine("...Oldest Published Date : " + r.oldest_published_date); 
       Console.WriteLine("...Oldest Published Date : " + r.newest_published_date); 
       Console.WriteLine("...Updated    : " + r.updated); 
       Console.WriteLine(); 
      } 
     } 
    } 
} 

輸出(僅前三個結果打印)

...List Name    : Combined Print and E-Book Fiction 
...Display Name   : Combined Print & E-Book Fiction 
...List Name Encoded  : combined-print-and-e-book-fiction 
...Oldest Published Date : 2011-02-13 
...Oldest Published Date : 2015-12-27 
...Updated    : WEEKLY 


...List Name    : Combined Print and E-Book Nonfiction 
...Display Name   : Combined Print & E-Book Nonfiction 
...List Name Encoded  : combined-print-and-e-book-nonfiction 
...Oldest Published Date : 2011-02-13 
...Oldest Published Date : 2015-12-27 
...Updated    : WEEKLY 


...List Name    : Hardcover Fiction 
...Display Name   : Hardcover Fiction 
...List Name Encoded  : hardcover-fiction 
...Oldest Published Date : 2008-06-08 
...Oldest Published Date : 2015-12-27 
...Updated    : WEEKLY 
+1

感謝!這很棒 :) –