简体   繁体   中英

RestSharp not deserializing a string (always null)

I currently tried to get serialized response from a RestSharp PostAsync call like in

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:

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? and my this be a bug in RestSharp?

Here is what "Data" looks like:

在此处输入图像描述

for restsharp you need JsonPropertyName attribute

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

or if you want to use JsonProperty you will have to use 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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