RestSharp Serialize JSON Array to request parameter

6k views Asked by At

I am not trying to post just JSON, but instead I want to post a JSON array to just one parameters of the post request.

Code:

        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);

AddObject fails, because I think the new RestSharp JSON serializer can't handle dictionaries. (error here: http://pastebin.com/PC8KurrW)

I tried also just request.AddParameter("locations", locations); but that doesnt serialize to json at all.

I want the request to look like

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

the [] is important to have, even its only 1 JSON object. It's an array of JSON objects.

1

There are 1 answers

1
Andrew Whitaker On BEST ANSWER

Not very slick, but this would work:

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);