如何在MVVm交叉Xamarin

时间:2017-05-14 09:24:23

标签: c# xamarin xamarin.android mvvmcross

我有什么:我有一个从API获得响应的服务

我想做什么

  • 我正在尝试将数据解析为模型对象(这里我有列表 人民)
  • 过去是来自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 个答案:

答案 0 :(得分:1)

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