var obj = new MyObject();
我在获取 RestSharp RestRequest.AddBody(obj)
时遇到问题;正确序列化对象。
class MyObject
{
[JsonProperty(PropertyName="a")]
public A{get;set;}
[JsonProperty(PropertyName="b")]
public B{get;set;}
}
问题是 AddBody 序列化程序没有考虑我的 JsonProperty 属性,我似乎可以弄清楚如何在 RestRequest 或 RestClient 上设置序列化程序?
最佳答案
我使用了 tafaju 的答案,并像这样实现了我的 json 序列化程序。
public class CustomJsonSerializer : ISerializer
{
public CustomJsonSerializer()
{
ContentType = "application/json";
}
public string Serialize(object obj)
{
return JsonConvert.SerializeObject(obj);
}
public string RootElement { get; set; }
public string Namespace { get; set; }
public string DateFormat { get; set; }
public string ContentType { get; set; }
}
它非常适合我,它读取属性并正确序列化所有类型。但我没有对所有类型进行测试。 文档说明 RootElement、Namespace、DateFormat 不用于 json。
关于c# - RestSharp RestRequest.AddBody 不使用 Newton.Json 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15371240/