参见英文答案 >
RestSharp serialization to JSON,object is not using SerializeAs attribute as expected 4个
我正在尝试与REST服务交谈,我正在尝试调用POST方法,我需要在帖子正文中提供一些数据.
我正在尝试与REST服务交谈,我正在尝试调用POST方法,我需要在帖子正文中提供一些数据.
我的模型类都很好地设置了这样的东西:
public class MyRequestClass { public string ResellerId { get; set; } public string TransactionId { get; set; } ... other properties of no interest here ... }
我在C#中使用RestSharp来调用我的REST服务:
RestClient _client = new RestClient(someUrl); var restRequest = new RestRequest("/post-endpoint",Method.POST); restRequest.RequestFormat = DataFormat.Json; restRequest.AddHeader("Content-Type","application/json"); restRequest.AddJsonBody(request); // of type "MyRequestClass" IRestResponse<MyResponse> response = _client.Execute<MyResponse>(restRequest);
一切似乎都很好 – 没有例外.但该服务响应:
We are experiencing problem in processing your request
当我查看正在发送的请求JSON时,我看到所有属性都是大写拼写:
{ "ResellerId":"123","TransactionId":"456" }
这导致了问题 – 服务以小写形式排除它们:
{ "resellerId":"123","transactionId":"456" }
所以我尝试用属性来装饰我的C#模型类:
public class MyRequestClass { [RestSharp.Serializers.SerializeAs(Name = "resellerId")] public string ResellerId { get; set; } [RestSharp.Serializers.SerializeAs(Name = "transactionId")] public string TransactionId { get; set; } ... other properties of no interest here ... }
解决方法
你不能或你应该将Json.NET添加到RestSharp.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。