C# RestSharp-如何影响JSON序列化(命名)?
我正在尝试与REST服务交谈,我正在尝试调用一个C# RestSharp-如何影响JSON序列化(命名)?,c#,json,restsharp,C#,Json,Restsharp,我正在尝试与REST服务交谈,我正在尝试调用一个POST方法,其中我需要在POST正文中提供一些数据 我的模型课都很好地设置了如下内容: public class MyRequestClass { public string ResellerId { get; set; } public string TransactionId { get; set; } ... other properties of no interest here ... } RestClien
POST
方法,其中我需要在POST正文中提供一些数据
我的模型课都很好地设置了如下内容:
public class MyRequestClass
{
public string ResellerId { get; set; }
public string TransactionId { get; set; }
... other properties of no interest here ...
}
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);
我在C#中使用RestSharp调用我的REST服务,如下所示:
public class MyRequestClass
{
public string ResellerId { get; set; }
public string TransactionId { get; set; }
... other properties of no interest here ...
}
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);
这就是问题的根源——该服务以小写字母将其排除在外:
{ "resellerId":"123","transactionId":"456" }
因此,我尝试用属性装饰我的C#model类:
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请求的属性名称是大写的,因此调用失败
我如何告诉RestSharp在C#model类生成的JSON中始终使用小写的属性名?编辑:这个答案已经过时了。阅读@marc_s分享的文章。我不会删除这个答案,因为它曾经很有用 您可以或者应该将Json.NET添加到RestSharp
上有一个问题。@CodeCaster:是的,似乎正在处理相同的问题。然而,如果你在那里移动你的答案,我将无法接受它:-(现在,RestSalk摆脱了Json.NET dependeny(按第103版()),这是今天要走的路吗?”Marcel,我不知道。