BahaiResearch.com BahaiResearch.com - 6 months ago 191
JSON Question

Deserializing a json string with newtonsoft or restsharp

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

I also have this problem, and I solve it using the Newtonsoft.Json.

Include theses namespaces:

using Newtonsoft.Json;
using RestSharp;

and try something like this:

return JsonConvert.DeserializeObject<T>(response.Content);

On the response.Content, you will have the raw result, so just deserialize this string to a json object. The T in the case is the type you need to deserialize. For sample:

var customerDto = JsonConvert.DeserializeObject<CustomerDto>(response.Content);