在我的代码中,我有一个向我的比特币服务器创建 JSON 请求的函数:
public static JObject InvokeMethod(string sMethod, params object[] parameters)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url);
webRequest.Credentials = Credentials;
webRequest.ContentType = "application/json-rpc";
webRequest.Method = "POST";
JObject joe = new JObject();
joe["jsonrpc"] = "1.0";
joe["id"] = "1";
joe["method"] = sMethod;
if (parameters != null)
{
if (parameters.Length > 0)
{
JArray props = new JArray();
foreach (var p in parameters)
{
props.Add(p);
}
joe.Add(new JProperty("params", props));
}
}
string s = JsonConvert.SerializeObject(joe);
[...]
当我通过它parameters
时:
int minconf, int maxconf, List<string> addresses
说
0, 9999, ["ms8G6E6J4no1KgRt9j3KyoniRgazRgDX1Q", "mzPs8fwypX8oiRGHjYHXMSbSoEdz1zgcAH"]
结果字符串s
如下所示:
"{"jsonrpc":"1.0","id":"1","method":"listunspent","params":[0,9999,"ms8G6E6J4no1KgRt9j3KyoniRgazRgDX1Q","mzPs8fwypX8oiRGHjYHXMSbSoEdz1zgcAH"]}"
而服务器期望的字符串应该如下所示:
"{"jsonrpc":"1.0","id":"1","method":"listunspent","params":[0,9999,["ms8G6E6J4no1KgRt9j3KyoniRgazRgDX1Q","mzPs8fwypX8oiRGHjYHXMSbSoEdz1zgcAH"]]}"
(注意列表周围的额外括号)
如何确保序列化对象采用我想要的格式?