简体   繁体   English

RestSharp 不反序列化字符串(始终为空)

[英]RestSharp not deserializing a string (always null)

I currently tried to get serialized response from a RestSharp PostAsync call like in我目前尝试从 RestSharp PostAsync 调用中获取序列化响应,例如

var responseData = Client.PostAsync<Data>(request).Result;

Now, this is what I receive:现在,这就是我收到的:

    {
    "status":1,
    "success":"message transmitted",
    "available":19215,
    "message_ids":"26684730:56798"
    }

and this is the "Data" class:这是“数据”class:

public class Data
{
    [JsonProperty("status")]
    public int Status { get; set; }

    [JsonProperty("success")]
    public string Success { get; set; }

    [JsonProperty("available")]
    public int Available { get; set; }

    [JsonProperty("message_ids")]
    public string MessageIds { get; set; }

    [JsonProperty("error")]
    public string Error { get; set; }

}

I don't know why, but the property message_ids is always null?: May this be caused by the, in the string?我不知道为什么,但是属性 message_ids 总是 null?: 这可能是由字符串中的, 引起的吗? and my this be a bug in RestSharp?我的这是RestSharp中的一个错误?

Here is what "Data" looks like:这是“数据”的样子:

在此处输入图像描述

for restsharp you need JsonPropertyName attribute对于restsharp,您需要 JsonPropertyName 属性

[JsonPropertyName("message_ids")]
public string MessageIds { get; set; }

or if you want to use JsonProperty you will have to use Newtonsoft.Json或者如果你想使用 JsonProperty 你将不得不使用 Newtonsoft.Json

var response = client.ExecuteAsync(request).Result;

//if you have async method better to use
var response = await client.ExecuteAsync(request);

Data  data = JsonConvert.DeserializeObject<Data>(response.Content);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM