gpt4 book ai didi

c# - RestSharp序列化JSON数组请求参数

转载 作者:太空宇宙 更新时间:2023-11-03 23:34:31 25 4
gpt4 key购买 nike

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

代码:

        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

[] 很重要,即使它只有一个 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);

关于c# - RestSharp序列化JSON数组请求参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30988496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com