簡體   English   中英

RestSharp序列化JSON數組以請求參數

[英]RestSharp Serialize JSON Array to request parameter

我不是只想發布JSON,而是想將JSON 數組發布到post請求的一個參數。

碼:

        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失敗,因為我認為新的RestSharp JSON序列化程序無法處理字典。 (這里的錯誤: http//pastebin.com/PC8KurrW

我也嘗試過request.AddParameter("locations", locations); 但這根本沒有序列化到json。

我希望請求看起來像

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

[]非常重要,甚至只有1個JSON對象。 它是一組JSON對象。

不是很光滑,但這可行:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM