Hel*_*mut 0 c# json restsharp deserialization json-deserialization
我目前尝试从 RestSharp PostAsync 调用获取序列化响应,如
var responseData = Client.PostAsync<Data>(request).Result;
Run Code Online (Sandbox Code Playgroud)
现在,这就是我收到的:
{
"status":1,
"success":"message transmitted",
"available":19215,
"message_ids":"26684730:56798"
}
Run Code Online (Sandbox Code Playgroud)
这是“数据”类:
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; }
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么,但属性message_ids始终为空!?这可能是由字符串中的 : 引起的,而这是 RestSharp 中的一个错误吗?
“数据”如下所示:
对于restsharp,您需要 JsonPropertyName 属性
[JsonPropertyName("message_ids")]
public string MessageIds { get; set; }
Run Code Online (Sandbox Code Playgroud)
或者如果您想使用 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);
Run Code Online (Sandbox Code Playgroud)