Search code examples
c#xamarinxamarin.androidmvvmcross

How to parse a network response to a Model class in MVVm cross Xamarin


What i have: I have a service that gets a response from a API


What i am trying to do:

  • I am trying to parse the data to a model object (Here I have list of peoples)
  • Being from a Android programmer in past, After getting a response from server i usually use GSON to populate model objects with data.
  • How to achieve the same in 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;
        }
    }
}

Output::

Currently in GetSalesPeopleAsync() is giving below output

[\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    ]

Solution

  • Use Newtonsoft.JsonConvert.DeserializeObject<People>(content);