Search code examples
c#.netjsondeserializationjson-deserialization

Deserialization using JsonConvert


I am trying to write a method which will accept a JSON string and Type. The aim of the method is to deserialize the string in the object specified by the type. I want the code to look like something below

public dynamic Deserialize(string serializedStr, Type type) {
      return JsonConvert.Deserialize<type>(serializedStr);
 }

Is it possible do such an operation using Newtonsoft.JSon?


Solution

  • All you have to do is:

    public dynamic Deserialize(string serializedStr, Type type)
    {
        return JsonConvert.DeserializeObject(serializedStr, type);
    }