反序列化未填充我的 variables/response.data 显示 null

Deserialization not filling my variables/response.data shows null

我想把我从 Rest API 得到的 jsonBody 放到这个 class 结构中。

Console.WriteLine(response.Data);

不知为何只有 return 为空。

这是我的全部代码:

RestClient client = new RestClient("https://euterpe.webuntis.com/WebUntis/jsonrpc.do?school=HTL-Perg");

public void login()
{
    var request = new RestRequest(Method.POST);
    request.AddHeader("Content-type", "application/jason");

    request.AddJsonBody(new
            {
                id = "ID",
                method = "authenticate",
                @params = new 
                {
                    user = "kung",
                    password = "foo",
                    client = "CLIENT"
                },
                jsonrpc = "2.0"
            });

    var response = this.client.Execute<AuthenticationResponse>(request);
    Console.WriteLine(response.Data);
}

请求有效,我得到以下 return 其余 API:

{
    "jsonrpc":"2.0",
    "id":"ID",
    "result": {
                 "sessionId":"A173BECFB75F66123D6B678BE9745A29",
                 "personType":5, 
                 "personId":1234,
                 "klasseId":123
              }
}

最后,这是应存储响应的两个 classes,但变量似乎保持为空:

    public class AuthenticationResponse
    {
        [DeserializeAs(Name = "jsonrpc")]
        public String Jsonrpc { get; set; }
        [DeserializeAs(Name = "id")]
        public int id { get; set; }
        [DeserializeAs(Name = "result", Content=true)]
        AuthenticationResult res { get; set; }
    }

    public class AuthenticationResult
    {
        [DeserializeAs(Name = "sessionId")]
        string sessionId { get; set; }
        [DeserializeAs(Name = "personType")]
        int personType { get; set; }
        [DeserializeAs(Name = "personId")]
        int personId { get; set; }
        [DeserializeAs(Name = "klasseId")]
        int klasseId { get; set; }
    }

感谢您的每一个建议。

您的 AuthenticationResponse 可能需要

public class AuthenticationResponse
    {
        [DeserializeAs(Name = "id")]
        public string id { get; set; }
        [DeserializeAs(Name = "result")]
        AuthenticationResult res { get; set; }

        public string jsonrpc { get; set; } // Can remove if you dont need it.
    }

更新 根据您认为这不起作用的评论,请尝试使用 Newtonsoft.Json 反序列化您的结果。

var response = client.Execute(request);
var deserialized = JsonConvert.DeserializeObject<AuthenticationResponse>(response.Content);

您必须在 class 定义中使用 JsonPropertyAttribute 而不是 DeserliazeAs。

public class AuthenticationResponse
{
        [JsonProperty("id")]
        public string id { get; set; }
        [JsonProperty("result")]
        AuthenticationResult res { get; set; }
}

public class AuthenticationResult
{
        [JsonProperty("sessionId")]
        string sessionId { get; set; }
        [JsonProperty("personType")]
        int personType { get; set; }
        [JsonProperty("personId")]
        int personId { get; set; }
        [JsonProperty("klasseId")]
        int klasseId { get; set; }
}

附加选项

您还可以使用 RestSharp.Newtonsoft.Json,它使 Newtonsoft.Json 成为 RestSharp 的默认序列化程序(并跳过中间步骤)。

在您的 Json 中,没有返回任何名为 "res" 的内容。 这应该可以解决您的问题:

public class AuthenticationResponse
    {
        [DeserializeAs(Name = "jsonrpc")]
        public String Jsonrpc {get;set;}
        [DeserializeAs(Name = "id")]
        public int Id { get; set; }
        [DeserializeAs(Name = "result")]
        AuthenticationResult Result { get; set; }
    }

我查看了您的代码并尝试使用 WebUntis。这是一个作为控制台应用程序的工作示例。特别重要的是调用 client.AddHandler 希望它能帮助解决你的问题。

using RestSharp;
using RestSharp.Deserializers;
using System;
using System.Text;

namespace ConsoleApp3
{
    internal class Program
    {
        private static RestClient client = new RestClient("https://euterpe.webuntis.com/WebUntis/jsonrpc.do?school=HTL-Perg");

        public class AuthenticationResponse
        {
            [DeserializeAs(Name = "id")]
            public string id { get; set; }
            [DeserializeAs(Name = "result", Content = true)]
            public AuthenticationResult result { get; set; }
        }

        public class AuthenticationResult
        {
            [DeserializeAs(Name = "sessionId")]
            public string sessionId { get; set; }
            [DeserializeAs(Name = "personType")]
            public int personType { get; set; }
            [DeserializeAs(Name = "personId")]
            public int personId { get; set; }
            [DeserializeAs(Name = "klasseId")]
            public int klasseId { get; set; }
        }

        private static void Main(string[] args)
        {
            client.AddHandler(new JsonDeserializer(), "application/json-rpc");

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-type", "application/json");

            request.AddJsonBody(new
            {
                id = "ID",
                method = "authenticate",
                @params = new
                {
                    user = "kung",
                    password = "foo",
                    client = "CLIENT"
                },
                jsonrpc = "2.0"
            });

            var response = client.Execute<AuthenticationResponse>(request);
            var resp = Encoding.UTF8.GetString(response.RawBytes, 0, (int)response.ContentLength);
            Console.WriteLine(response.Data);
        }
    }
}

您好, 莱纳.