RestSharp 序列化 JSON 数组以请求参数

Par*_*roX 1 c# serialization json restsharp

我不是想只发布 JSON,而是想将 JSON数组发布到发布请求的一个参数。

代码:

        var locations = new Dictionary<string, object>();
        locations.Add("A", 1);
        locations.Add("B", 2);
        locations.Add("C", 3);

        request.AddObject(locations);
        request.AddParameter("date", 1434986731000);
Run Code Online (Sandbox Code Playgroud)

AddObject 失败,因为我认为新的 RestSharp JSON 序列化程序无法处理字典。(这里的错误:http : //pastebin.com/PC8KurrW

我也尝试过,request.AddParameter("locations", locations); 但根本没有序列化为 json。

我希望请求看起来像

locations=[{A:1, B:2, C:3}]&date=1434986731000

[]很重要,即使它只有 1 个 JSON 对象。它是一个 JSON 对象数组。

And*_*ker 5

不是很光滑,但这会起作用:

var request = new RestSharp.RestRequest();

var locations = new Dictionary<string, object>();
locations.Add("A", 1);
locations.Add("B", 2);
locations.Add("C", 3);

JsonObject o = new JsonObject();

foreach (var kvp in locations)
{
    o.Add(kvp);
}

JsonArray arr = new JsonArray();
arr.Add(o);

request.AddParameter("locations", arr.ToString());
request.AddParameter("date", 1434986731000);
Run Code Online (Sandbox Code Playgroud)