使用newtonsoft或restsharp反序列化json字符串 [英] Deserializing a json string with newtonsoft or restsharp

查看:604
本文介绍了使用newtonsoft或restsharp反序列化json字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自Json格式的数据库的字符串.

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)

但是.Deserialize函数需要一个IRestResponse

有没有一种使用RestSharp来反序列化原始字符串的方法?

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

推荐答案

我也有这个问题,我使用 Newtonsoft.Json .

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

包括以下名称空间:

using Newtonsoft.Json;
using RestSharp;

并尝试这样的事情:

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

response.Content上,您将获得原始结果,因此只需将此字符串反序列化为json对象.在这种情况下,T是您需要反序列化的类型.例如:

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

这篇关于使用newtonsoft或restsharp反序列化json字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆