Deserializing a json string with newtonsoft or restsharp

Ian Vink picture Ian Vink · May 13, 2013 · Viewed 76.7k times · Source

I have a string that comes out of a database which is in Json format.

I have tried to deserialize it with:

RestSharp.Deserializers.JsonDeserializer deserial = new JsonDeserializer();
var x = deserial .Deserialize<Customer>(myStringFromDB)

But the .Deserialize function expects an IRestResponse

Is there a way to use RestSharp to just deserialize raw strings?

Answer

StevieJ81 picture StevieJ81 · Aug 8, 2013

If you want to avoid using extra libraries, try this:

RestSharp.RestResponse response = new RestSharp.RestResponse();

response.Content = myStringFromDB; 

RestSharp.Deserializers.JsonDeserializer deserial = new JsonDeserializer();

Customer x = deserial.Deserialize<Customer>(response);

Caveats apply - not extensively tested - but seems to work well enough.