代码之家  ›  专栏  ›  技术社区  ›  newbyca

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

  •  0
  • newbyca  · 技术社区  · 7 年前

    使用RestSharp,我需要发布一个包含json字符串的正文,如下所示:

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

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

    在这种情况下,向“a”属性添加“$”的最佳方法是什么?

    1 回复  |  直到 7 年前
        1
  •  2
  •   dbc    7 年前

    由于您使用的是匿名类型,因此可以很容易地切换到使用字典:

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

    如果使用显式类型,可以检查 RestSharp serialization to JSON, object is not using SerializeAs attribute as expected 用于选项。