Deserializing a json string with newtonsoft or restsharp

北城余情 提交于 2019-11-29 22:16:42

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);

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!