C# 在从 JSON reader 读取时发现 属性 'InputArguments' 的意外 'StartObject' 节点。需要一个 'PrimitiveValue' 节点

C# An unexpected 'StartObject' node was found for property 'InputArguments' when reading from the JSON reader. A 'PrimitiveValue' node was expected

我正在使用 RestClient 将 JSON 参数传递给 C# 中的 api。但是我得到了响应

"An unexpected 'StartObject' node was found for property named 'InputArguments' when reading from the JSON reader. A 'PrimitiveValue' node was expected"

我在 C# 中使用以下代码

var client_startRobot = new RestClient("https://xxxx.xxxx.com/odata/Jobs/UiPath.Server.Configuration.OData.StartJobs");
var request_startRobot = new RestRequest(Method.POST) ;
request_startRobot.AddParameter("Authorization", string.Format("Bearer " + result), ParameterType.HttpHeader);
request_startRobot.AddHeader("content-type", "application/json");
string parameter = "{\"startInfo\":{\"ReleaseKey\": \"ds32rd1-6c98-42f542d-23bb8111ac91d\",\"RobotIds\": [1],\"JobsCount\": 0,\"Strategy\": \"Specific\",\"InputArguments\": {\"add_name\": \"xxxxx-xxx-\"}}}";
request_startRobot.AddParameter("application/json; charset=utf-8", parameter, ParameterType.RequestBody);
IRestResponse response_startRobot = client_startRobot.Execute(request_startRobot);

这好像是仔细阅读API文档的问题。假设您正在尝试按照 here 所述调用编排器,我发现这个示例与您的非常相似。

{ "startInfo":
   { "ReleaseKey": "5b754c63-5d1a-4c37-bb9b-74b69e4934bf",
     "Strategy": "Specific",
     "RobotIds": [ 1553 ],
     "NoOfRobots": 0,
     "Source": "Manual",
     "InputArguments": "{\"message\":\"Aloha\"}"
   } 
}

请注意,InputArguments 值实际上是一个简单的字符串,不是实际的JSON(该字符串包含转义的JSON 字符串)。

您的请求如下所示:

"InputArguments": {"add_name": "xxxxx-xxx-"}

根据给出的例子,应该是这样的:

"InputArguments": "{\"add_name\": \"xxxxx-xxx-\"}"

看来您必须 "double escape" 字符串的这一部分,如下所示:

\"InputArguments\": \"{\\"add_name\\": \\"xxxxx-xxx-\\"}\"

实际上构建一个强类型请求对象并将序列化留给您的 REST 客户端可能会使事情更容易阅读。