I am working on windows phone 8.1 application. I am working with REST API and the the api return me JSON data. I want to create a method such that each time I call the API, I just send the type of class on which it is to be deserialized as a parameter and I get the deserialized data into an object of that model. My current code looks like this:
public static async Task GetAPIData(Type referanceModel, string serviceUrl)
{
HttpClient client = new HttpClient();
var responce = await client.GetAsync(new Uri(serviceUrl));
JArray arr = JArray.Parse(await response.Content.ReadAsStringAsync());
foreach (JObject obj in arr.Children<JObject>())
{
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.NullValueHandling = NullValueHandling.Ignore;
settings.MissingMemberHandling = MissingMemberHandling.Ignore;
var rcvdData = JsonConvert.DeserializeObject<referanceModel>(obj.ToString(), settings);
//var method = typeof(JsonConvert).GetTypeInfo().GetDeclaredMethods("DeserializeObject").ToList()[5].MakeGenericMethod(ReferanceModel);
//var rcvd = method.Invoke(null, new object[] { obj.ToString(), settings });
}
}
The above code shows me the error that referanceModel is a variable. I tried the commented code also but it doesn't work too.
The two classes are: AllvehData and AllDevicesData
public class AllVehData
{
public string Colour { get; set; }
public string Make { get; set; }
public string model { get; set; }
public string Year { get; set; }
}
public class AllDevicesData
{
bool has_tracking_device { get; set; }
int drive_type_name_string_id { get; set; }
public int vehicle_status { get; set; }
public int cargo_capacity { get; set; }
public int fuel_capacity { get; set; }
}
I searched a lot on the topic and found this but failed to understand. I need help.
Aucun commentaire:
Enregistrer un commentaire