我尝试序列化一个自定义对象,但结果是空的。
我想要的结果是:
{
"accessRules": [
{
"method": "GET",
"path": "/*"
}
],
"redirection":"https://www.mywebsite.com/"
}'
以下是我的课程:
[Serializable]
public class AccessRule
{
[SerializeAs(Name = "method")]
string Method { get; set; }
[SerializeAs(Name = "path")]
string Path { get; set; }
public AccessRule(string method, string path)
{
this.Method = method;
this.Path = path;
}
}
[Serializable]
public class RequestBody
{
[SerializeAs(Name = "accessRules")]
AccessRule[] AccessRules
{
get { return arList.ToArray(); }
}
private List<AccessRule> arList;
[SerializeAs(Name = "redirection")]
string Redirection { get; set; }
public RequestBody(string method, string path, string redirection)
{
this.arList = new List<AccessRule>();
this.arList.Add(new AccessRule(method, path));
this.Redirection = redirection;
}
}
var RequestBody = new RequestBody("GET", "/*", "https://www.mywebsite.com");
它导致一个空的json字符串。
如果我这样做:
var RequestBody = new { accessRules = new[] { new { method = "GET", path = "/*" } }, redirection = "https://www.mywebsite.com" };
有效。
我使用RestSharp 104.4.0.0,也尝试使用Newtonsoft.Json v6.0.0.0
我在这里缺少什么?
感谢。