Hi I have the following code to get data from a REST service:
HttpResponseMessage response;
response = client.GetAsync("CatalogUpdate/" + sessionId).Result;
if (response.IsSuccessStatusCode)
{
catalogs = response.Content.ReadAsAsync<List<Models.CatalogInfo>>().Result;
}
My CatalogInfo class is:
public class CatalogInfo
{
public CatalogInfo(int id,string name,string date)
{
this.ID = id;
this.Name = name;
this.Date = date;
}
public int ID { get; set; }
public string Name { get; set; }
public string Date { get; set; }
}
And the jSON that Im getting from the REST service is:
{"error":false,"locations":[{"ID":"3","ABC":"XC","Description":"Rome","Status":"1"},{"ID":"4","CD1":"XH","Description":"Italy","Status":"1"}]}
I want to map the jSON to my CatalogInfo
class, is there a way to do this?
The easiest option here is to use Json.NET and to create classes that represent the expected JSON, so for example:
We don't have to implement every property as Json.NET will just ignore what isn't there.
Then deserialize the response. In your case you're using
HttpResonseMessage
so something like this:You can then use LINQ to convert the locations over to your object: