【问题标题】:Deserialize a JsonResult from C#从 C# 反序列化 JsonResult
【发布时间】:2022-07-19 15:39:49
【问题描述】:

我使用此代码访问 REST API:

public async Task<IActionResult> NetExplorerAPI(string AdresseAPI, object Requete, RestSharp.Method Methode)
    {
        var client = new RestClient(AdresseAPI);

        var request = new RestRequest();
        request.Method = RestSharp.Method.Post;
        request.AddJsonBody(Requete);
        request.AddHeader("Accept", "application/json");
        
        //request.AddHeader("Authorization", "Bearer a844024a4e744182aeaa62dd6347b9049f9ba35650339d2b9362e1bf03a92ac0");
        //IRestResponse response = await client.ExecuteAsync(request);

        RestResponse response = await client.ExecuteAsync(request);
        
        JsonResult jr = new JsonResult(response);
        
        return (jr);
    }

我想反序列化 JsonResult 以获取字符串中的令牌:

{ "token": "med6RRIikrZ-2tua9jUa6pVZubnPvhqSH6wHvtkH42TNfJGXOaI-GioUKPvfbhP7XiGG6UgjCzUnJt87kwsljQBAKEb" }

但是当我序列化 JsonResult 时,我得到了很多我不需要的项目:

string s = JsonConvert.SerializeObject(jr);

{"ContentType":null,"SerializerSettings":null,"StatusCode":null,"Value":{"ContentType":null,"SerializerSettings":null,"StatusCode":null,"Value":{ "请求":{"AlwaysMultipartFormData":false,"MultipartFormQuoteParameters":false,"FormBoundary":null,"Parameters":[{"DataFormat":0,"ContentEncoding":null,"Name":"","Value ":{"user":"sylvain.krier@protonmail.com","password":"S@r@line2004"},"Type":3,"Encode":false,"ContentType":"application/json "},{"Name":"Accept","Value":"application/json","Type":2,"Encode":false,"ContentType":null}],"Files":[]," Method":1,"Timeout":0,"Resource":"","RequestFormat":0,"RootElement":null,"OnBeforeDeserialization":null,"OnBeforeRequest":null,"OnAfterRequest":null,"Attempts ":1,"CompletionOption":0,"ResponseWriter":null,"AdvancedResponseWriter":null},"ContentType":"application/json","ContentLength":103,"ContentEncoding":[],"内容":"{"token":"wlK4LIRpOxqKOwJ2Hs554l5-WI--IrqHW7TECZ3YtdS-RpzDuQGaQeLI0qjo8NzaSPhCUYaarBcXstrI5sPlXkwCmk9"}","StatusCode": 200,"IsSuccessful":true,"StatusDescription":"Ok","RawBytes":"eyJ0b2tlbiI6IndsSzRMSVJwT3hxS093SjJIczU1NGw1LVdJLS1JcnFIVzdURUNaM1l0ZFMtUnB6RHVRR2FRZUxJMHFqbzhOemFTUGhDVVlhYXJCY1hzdHJJNXNQbFhrd0NtazkifQ==","ResponseUri":"https://patrimoine-click.netexplorer.pro/api/auth","Server ":"Apache","Cookies":[],"Headers":[{"Name":"Date","Value":"Tue, 19 Jul 2022 06:40:36 GMT","Type":2 ,"Encode":false,"ContentType":null},{"Name":"Server","Value":"Apache","Type":2,"Encode":false,"ContentType":null}, {"Name":"Pragma","Value":"no-cache","Type":2,"Encode":false,"ContentType":null},{"Name":"Cache-Control"," Value":"no-store, must-revalidate, no-cache","Type":2,"Encode":false,"ContentType":null},{"Name":"X-NetExplorer-Version","值":"7.4.4.12","Type":2,"Encode":false,"ContentType":null},{"Name":"Access-Control-Allow-Origin","Value":"*" ,"Type":2,"Encode":false,"ContentType":null},{"Name":"X-UA-Compatible","Value":"IE=edge,chrome=1","Type" :2,"Encode":false,"ContentType":null},{"Name":"Connection","Value":"cl ose","Type":2,"Encode":false,"ContentType":null},{"Name":"X-Content-Type-Options","Value":"nosniff","Type":2 ,"Encode":false,"ContentType":null},{"Name":"Transfer-Encoding","Value":"chunked","Type":2,"Encode":false,"ContentType":null }],"ContentHeaders":[{"Name":"Expires","Value":"Thu, 19 Nov 1981 08:52:00 GMT","Type":2,"Encode":false,"ContentType" :null},{"Name":"Content-Type","Value":"application/json","Type":2,"Encode":false,"ContentType":null},{"Name":" Content-Length","Value":"103","Type":2,"Encode":false,"ContentType":null}],"ResponseStatus":1,"ErrorMessage":null,"ErrorException":null ,"版本":"1.1","RootElement":null}}}

我不知道如何获取“内容”项。

提前致谢,

【问题讨论】:

标签: c# json .net restsharp


【解决方案1】:

来自RestSharp的官方QuickStart

使用类型化的ExecuteAsync&lt;T&gt; 时,您会得到一个RestResponse&lt;T&gt; 的实例,它与RestResponse 相同,但还包含带有反序列化响应的T Data 属性。

如果远程服务器返回错误,任何ExecuteAsync 重载都不会抛出。您可以检查响应并找到状态代码、错误消息以及可能的异常。

GetAsync&lt;T&gt; 这样的扩展不会返回整个RestResponse&lt;T&gt;,而只是一个反序列化的响应。如果远程服务器返回错误,这些扩展将引发异常。异常会告诉你服务器返回了什么状态码。

看来你必须做出选择:

  1. 使用client.ExecuteAsync,然后使用response.Data
  2. 使用client.GetAsync&lt;T&gt;

对于 client.GetAsync,您需要一个新类型。例如

public class MyTokenClass { public string token {get; set;} } 

然后是var t = await client.GetAsync&lt;MyTokenClass&gt;()

【讨论】:

    猜你喜欢
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    • 2013-02-09
    相关资源
    最近更新 更多