C#从JSON阅读器读取时,为“InputArguments”属性找到了一个意外的“StartObject”节点。预期'PrimitiveValue'节点

问题描述 投票:0回答:1

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

“从JSON读取器读取时,为名为'InputArguments'的属性找到了一个意外的'StartObject'节点。预计'PrimitiveValue'节点”

我在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);
c# json rest-client uipath
1个回答
2
投票

这似乎是一个仔细阅读API文档的问题。假设你试图像here所描述的那样调用一个orchestrator,我发现这个看起来很像你的例子。

{ "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-\"}"

看起来你必须“双重逃避”你的字符串的这一部分,如下所示:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.