Deserializing a json string with restsharp
Solution 1
There are sereval ways to do this. A very popular library to handle json is the Newtonsoft.Json
. Probably you already have it on your asp.net project but if not, you could add it from nuget
.
Considering you have a response object, include the following namespaces and call the static method DeserializeObject<T>
from JsonConvert
class:
using Newtonsoft.Json;
using RestSharp;
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 example:
var customerDto = JsonConvert.DeserializeObject<CustomerDto>(response.Content);
Update
Recently, Microsoft has added a namespace System.Text.Json
which handle json format on the .Net platform. You could use it calling the JsonSerializer.Deserialize<T>
static method:
using System.Text.Json;
var customer = JsonSerializer.Deserialize<Customer>(jsonContent);
Solution 2
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.
Related videos on Youtube
Ian Vink
https://mvp.microsoft.com/en-us/PublicProfile/5002789?fullName=Ian%20Vink
Updated on April 30, 2021Comments
-
Ian Vink about 3 years
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 anIRestResponse
Is there a way to use RestSharp to just deserialize raw strings?
-
Andy about 7 yearsI feel your pain
-
-
Ian Vink almost 11 yearsThe RestSharp serializer and deserializer was the problem. By using Newton soft the problem went away.
-
Matthew over 8 years@BahaiResearch.com It's not that RestSharp's serializer/deserializer has/is a problem per se; it just doesn't have the functionality you need, since RestSharp is an http client library not a general serialization tool. As you pointed out it requires an
IRestResponse
, as opposed to let's say supporting an IRestResponse.Content (string type) as well. StevieJ81 below points out a potential way workaround if for some reason you want to or must use RestSharp for json deserialization: he directly plugs IRestResponse.Content. -
Brad Bruce over 7 yearsWorks great. One less library I have to include in my application.
-
Michael over 4 yearsIn the version of RestSharp I'm using it looks like the JsonDeserializer class has been moved to RestSharp.Serialization.Json.JsonDeserializer
-
Esteban Verbel over 4 yearsI don't think this is valid anymore, at least for version "100.6.10"
-
Alexis Leclerc over 4 yearsIn my case the RestSharp deserializer handles funky object names in the json content much better than the Newtonsoft deserializer, so that's another argument not to include another library just for this.