2017-05-14 16 views
0

我有什麼:我有一個會從一個API如何解析網絡響應MVVM模型的類交叉Xamarin


什麼,我試圖做響應的服務:

  • 我試圖將數據解析成一個模型對象(這裏我有 人民的列表)
  • 在過去從Android的程序員作爲,前前後後呃從服務器獲得響應 我通常使用GSON來使用數據填充模型對象。
  • 如何實現相同的MvvmCross xamarin

RestService.cs

public class RestService : IntIRestService 
    { 
     public async Task<List<People>> GetSalesPeopleAsync() 
     { 
      try 
      { 
       //Declare a Http client 
       var client = new HttpClient(); 
       //Add a Base URl 
       client.BaseAddress = new Uri(Constants.MUrl); 
       //Add the response type 
       client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
       //Add the API 
       Task<string> response = client.GetStringAsync("iCodersLab/Custom-ListView-Using-Volley/master/richman.json"); 

       String content = await response; 

       //var list = JsonConvert.DeserializeObject<List<People>>(content).ToList(); 

       //return content; 
      } 
      catch (Exception ex) 
      { 
       var t = ex.Message; 
      } 

      return null; 
     } 
    } 

People.cs

namespace SqliteDemo.core.Models 
{ 
    public class People 
    { 
     public String name 
     { 
      get; 
      set; 
     } 

     public String image 
     { 
      get; 
      set; 
     } 

     public String worth 
     { 
      get; 
      set; 
     } 

     public int InYear 
     { 
      get; 
      set; 
     } 

     public String source 
     { 
      get; 
      set; 
     } 
    } 
} 

輸出::

目前GetSalesPeopleAsync()是給下面的輸出

[\n{\n  \"name\": \"First Richman\",\n  \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/Bill_Gates.jpg\",\n  \"worth\": \"$89 billion\",\n  \"InYear\": 2015,\n  \"source\": \"Microsoft\"\n },\n {\n  \"name\": \"Second Richman\",\n  \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/Carlos_Slim_Helu.jpg\",\n  \"worth\": \"$85 billion\",\n  \"InYear\": 2015,\n  \"source\": \"Telmex, Grupo Carso\"\n },\n {\n  \"name\": \"Third Richman\",\n    \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/WarrenBuffett.jpg\",\n    \"worth\": \"$77 billion\",\n    \"InYear\": 2015,\n    \"source\": \"Berkshire Hathaway\"\n },\n {\n  \"name\": \"Fourth Richman\",\n    \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/amancio_ortega.jpg\",\n    \"worth\": \"$70 billion\",\n    \"InYear\": 2015,\n    \"source\": \"Inditex Group\"\n },\n {\n  \"name\": \"Fifth Richman\",\n    \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/Larry_Elllison.jpg\",\n    \"worth\": \"$65 billion\",\n    \"InYear\": 2015,\n    \"source\": \"Oracle Corporation\"\n },\n {\n  \"name\": \"Sixth Richman\",\n    \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/cgkochwide.jpg\",\n    \"worth\": \"$45 billion\",\n    \"InYear\": 2015,\n    \"source\": \"Koch Industries\"\n },\n {\n  \"name\": \"Seventh Richman\",\n    \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/David_Kochcrop2007.jpg\",\n    \"worth\": \"$42 billion\",\n    \"InYear\": 2015,\n    \"source\": \"Koch Industries\"\n },\n {\n  \"name\": \"Christy Walton\",\n    \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/christywalton.jpg\",\n    \"worth\": \"$41.7 billion \",\n    \"InYear\": 2015,\n    \"source\": \"Wal-Mart\"\n },\n {\n  \"name\": \"Jim Walton\",\n    \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/Jim_Walton.jpg\",\n    \"worth\": \"$40.6 billion\",\n    \"InYear\": 2015,\n    \"source\": \"Wal-Mart\"\n },\n {\n  \"name\": \"Liliane Bettencourt\",\n    \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/lilianebettencourt.jpg\",\n    \"worth\": \"$40.1 billion\",\n    \"InYear\": 2015,\n    \"source\": \"L'Oreal\"\n },\n {\n  \"name\": \"Carlos Slim & family\",\n    \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/CarlosSlimfamily.jpg\",\n    \"worth\": \"$79.2 billion\",\n    \"InYear\": 2014,\n    \"source\": \"Telmex, Am�rica M?vil, Grupo Carso\"\n },\n {\n  \"name\": \"Bernard Arnault\",\n    \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/Bernard_Arnault.jpg\",\n    \"worth\": \"$29.0 billion\",\n    \"InYear\": 2013,\n    \"source\": \"LVMH Mo�t Hennessy � Louis Vuitton\"\n },\n {\n  \"name\": \"Prince Al-Waleed\",\n    \"image\": \"https://raw.githubusercontent.com/iCodersLab/Custom-ListView-Using-Volley/master/images/PrinceAlwaleed.jpg\",\n    \"worth\": \"$23.7 billion\",\n    \"InYear\": 2005,\n    \"source\": \"Kingdom Holding Company\"\n }\n ] 

回答

1

使用Newtonsoft.JsonConvert.DeserializeObject<People>(content);

+0

什麼是'Newtonsoft'是圖書館嗎? 我們是否需要在覈心項目中爲此添加任何'Nuget'包? – Devrath

+1

https://www.nuget.org/packages/newtonsoft.json/ –

+0

謝謝,我需要傳遞給'content'的什麼東西,我可以通過**輸出**我收到 您可以添加一些代碼&return type – Devrath