1

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. Both of these classes are inside a singleton class called ThisUserClass with the instance of ThisUser

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.

Community
  • 1
  • 1
iam.Carrot
  • 4,976
  • 2
  • 24
  • 71

1 Answers1

0

In this case you don't need the generic overload JsonConvert.DeserializeObject<>(string, JsonSerializerSettings). Instead use the JsonConvert.DeserializeObject(string, Type, JsonSerializerSettings) overload and do:

var rcvdData = JsonConvert.DeserializeObject(obj.ToString(), referanceModel, settings);

When you use generics - either class or method - you MUST pass an actual type in the place of a generic parameter as opposed of a variable of type Type. This is the reason you get compile time error. E.g. in order to initialize a list of strings you cannot do:

var theStringVar = string.Empty;
var myList1 = new List<theStringVar>(); // Here we get the compile time error.

// OR

var theTypeVar = typeof(string);
var myList2 = new List<theTypeVar>(); // That won't work either.

Instead you need to specify the string type explicitly:

var myList3 = new List<string>(); // This works just fine.
Bozhidar Stoyneff
  • 3,576
  • 1
  • 18
  • 28