简体   繁体   English

使用RestSharp,如何在RestRequest.AddJsonBody()中包含诸如“ $ fieldname”之类的字段名?

[英]Using RestSharp, how to include a fieldname like “$fieldname” in RestRequest.AddJsonBody()?

Using RestSharp, I need to POST a body containing a json string that looks like this: 使用RestSharp,我需要发布一个包含如下所示json字符串的主体:

{
    "$a": "b",
    "c": "d"
}

In the past I've created RestSharp requests using code like this: 过去,我使用以下代码创建RestSharp请求:

var request = new RestRequest("someApiEndPoint", RestSharp.Method.POST);
request.AddJsonBody(new
{
    a = "b",
    c = "d"
});

What's the best way to add a "$" to the "a" property in this case? 在这种情况下,向“ a”属性添加“ $”的最佳方法是什么?

Since you are using an anonymous type, you could just as easily switch to using a dictionary: 由于您使用的是匿名类型,因此可以轻松地切换到使用字典:

var root = new Dictionary<string, object>
{
    {"$a", "b" },
    {"c", "d" },
};
var request = new RestRequest("someApiEndPoint", RestSharp.Method.POST)
    .AddJsonBody(root);

If you were using an explicit type, you could check RestSharp serialization to JSON, object is not using SerializeAs attribute as expected for options. 如果使用显式类型,则可以检查RestSharp序列化为JSON,对象未按预期使用选项的SerializeAs属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM