我正在尝试使用RestSharp使用自定义序列化器反序列化请求的一部分。我创建一个新的rest客户并添加我的客户序列化器
protected void SetJsonHandler(IRestClient client)
{
var jsonHandlerType = "application/json";
client.ClearHandlers();
var jsonDeserializer = new JsonNetDeseralizer(JsonSettings);
client.AddHandler(jsonHandlerType, jsonDeserializer);
}
我请求获取看起来像这样的帐户:
public List<Account> GetAccounts()
{
var request = new RestRequest("accounts/", Method.GET, DataFormat.Json);
var response = this._client.Execute<List<Account>>(request);
this.HandleKnownExceptions(response);
return response.Data;
}
我有与属性名称匹配的模型
public class Balence
{
public Double Amount { get; set; }
public Currency Currency { get; set; }
}
public class Account
{
public string Id { get; set; }
public string Name { get; set; }
public bool Primary { get; set; }
public AccountType Type { get; set; }
public Balence Balence { get; set; }
public DateTime CreatedAt { get; set; }
}
但它们不会映射,因为模型嵌套在data属性中
{
"pagination":{
"ending_before":null,
//...
},
"data":[
{
"id":"gje99c1e-7081-5784-9a1e-4p302b92312b",
"name":"ETH Wallet",
"primary":false,
"type":"wallet",
"balance":{
"amount":"0.97845347",
"currency":"ETH"
},
"created_at":"2017-10-01T16:50:51Z",
"resource":"account",
}],
}
编辑
Json反序列化器:
public class JsonNetDeseralizer : IDeserializer
{
private readonly JsonSerializerSettings settings;
public string RootElement { get; set; }
public string Namespace { get; set; }
public string DateFormat { get; set; }
public JsonNetDeseralizer(JsonSerializerSettings settings)
{
this.settings = settings;
}
public T Deserialize<T>(IRestResponse response)
{
return JsonConvert.DeserializeObject<T>(response.Content, settings);
}
}
我希望不必使用具有data属性的虚拟模型来进行映射。有没有一种简单的方法可以配置Rest Sharp,Json.Net来将数据属性映射到
中的模型