如何在 C# 中的 JSON-RPC 2.0 中编写 "params"?
How to write "params" in JSON-RPC 2.0 in C#?
我想使用JSON-RPC 来控制名为aria2 的应用程序。当它不需要参数时我可以控制它。但是我尝试了很多方法,我从来没有成功地用params控制它。
我试过的一些代码是这样的:
if (secret != null && secret != "")
json = JsonConvert.SerializeObject(new JObject { ["jsonrpc"] = "2.0", ["id"] = "m", ["method"] = "aria2.addUri", ["params"] = { "token:" + secret, "[http://csharp.org/file.zip]" } });
else
json = JsonConvert.SerializeObject(new JObject { ["jsonrpc"] = "2.0", ["id"] = "m", ["method"] = "aria2.addUri", ["params"] = @"[http://csharp.org/file.zip]" });
我也试过:
if (secret != null && secret != "")
string json = "{\"jsonrpc\": \"2.0\",\"method\": \"aria2.addUri\",\"params\": {\"token:\"" + secret + "\",\"http://csharp.org/file.zip\"},\"id\": \"m\"}";
else
string json = "{\"jsonrpc\": \"2.0\",\"method\": \"aria2.addUri\",\"params\": {\"http://csharp.org/file.zip\"},\"id\": \"m\"}";
而且我已经尝试了很多 [{'"
的组合和排列,但没有任何效果。
Here is the RPC guide about aria2 for Python:
https://aria2.github.io/manual/en/html/aria2c.html#rpc-authorization-secret-token
这里是一些初学者可能想知道的解决方案。
First, know what you want to output, in this case is:
{"jsonrpc":"2.0","id":"m","method":"aria2.addUri","params":["token:secret",["http://csharp.org/file.zip"]]}
结果在这里:http://jsoneditoronline.org/?id=4ee8fb1e0314e124bd3ab7d4b2ed19f1
然后,小提示 []
在参数值之外,所以它们是数组,而不是字符串。它不能使用["params"] = {}
,也不会覆盖字符串到数组,例如下面的错误代码:
JsonConvert.SerializeObject(new JObject { ["params"] = "[\"token:secret\", [\"http://csharp.org/file.zip\"]]" });
只得到:
{"params":"[\"token:secret\", [\"http://csharp.org/file.zip\"]]"}
The most important is the format of token, it's not a
JProperty()
in JObject()
, it's just a string in params's
JArray()
. And uri is also in params's JArray()
's JArray()
. So
the right version is:
JArray param = new JArray { "token:secret", new JArray { "http://csharp.org/file.zip" } };
string json = JsonConvert.SerializeObject(new JObject { ["jsonrpc"] = "2.0", ["id"] = "m", ["method"] = "aria2.addUri", ["params"] = param });
JArray()
是[]
,JObject()
是{}
; JArray()
≠ JObject()
.
If we don't need JsonConvert()
, the right version is easy:
string json = "{ \"jsonrpc\": \"2.0\", \"id\": \"m\", \"method\": \"aria2.addUri\", \"params\": [\"token:secret\", [\"http://csharp.org/file.zip\"]] }";
- 在这种情况下,我们无法将
"
更改为 '
。
我想使用JSON-RPC 来控制名为aria2 的应用程序。当它不需要参数时我可以控制它。但是我尝试了很多方法,我从来没有成功地用params控制它。
我试过的一些代码是这样的:
if (secret != null && secret != "")
json = JsonConvert.SerializeObject(new JObject { ["jsonrpc"] = "2.0", ["id"] = "m", ["method"] = "aria2.addUri", ["params"] = { "token:" + secret, "[http://csharp.org/file.zip]" } });
else
json = JsonConvert.SerializeObject(new JObject { ["jsonrpc"] = "2.0", ["id"] = "m", ["method"] = "aria2.addUri", ["params"] = @"[http://csharp.org/file.zip]" });
我也试过:
if (secret != null && secret != "")
string json = "{\"jsonrpc\": \"2.0\",\"method\": \"aria2.addUri\",\"params\": {\"token:\"" + secret + "\",\"http://csharp.org/file.zip\"},\"id\": \"m\"}";
else
string json = "{\"jsonrpc\": \"2.0\",\"method\": \"aria2.addUri\",\"params\": {\"http://csharp.org/file.zip\"},\"id\": \"m\"}";
而且我已经尝试了很多 [{'"
的组合和排列,但没有任何效果。
Here is the RPC guide about aria2 for Python: https://aria2.github.io/manual/en/html/aria2c.html#rpc-authorization-secret-token
这里是一些初学者可能想知道的解决方案。
First, know what you want to output, in this case is:
{"jsonrpc":"2.0","id":"m","method":"aria2.addUri","params":["token:secret",["http://csharp.org/file.zip"]]}
结果在这里:http://jsoneditoronline.org/?id=4ee8fb1e0314e124bd3ab7d4b2ed19f1
然后,小提示 []
在参数值之外,所以它们是数组,而不是字符串。它不能使用["params"] = {}
,也不会覆盖字符串到数组,例如下面的错误代码:
JsonConvert.SerializeObject(new JObject { ["params"] = "[\"token:secret\", [\"http://csharp.org/file.zip\"]]" });
只得到:
{"params":"[\"token:secret\", [\"http://csharp.org/file.zip\"]]"}
The most important is the format of token, it's not a
JProperty()
inJObject()
, it's just a string in params'sJArray()
. And uri is also in params'sJArray()
'sJArray()
. So the right version is:
JArray param = new JArray { "token:secret", new JArray { "http://csharp.org/file.zip" } };
string json = JsonConvert.SerializeObject(new JObject { ["jsonrpc"] = "2.0", ["id"] = "m", ["method"] = "aria2.addUri", ["params"] = param });
JArray()
是[]
,JObject()
是{}
;JArray()
≠JObject()
.
If we don't need
JsonConvert()
, the right version is easy:
string json = "{ \"jsonrpc\": \"2.0\", \"id\": \"m\", \"method\": \"aria2.addUri\", \"params\": [\"token:secret\", [\"http://csharp.org/file.zip\"]] }";
- 在这种情况下,我们无法将
"
更改为'
。