【问题标题】:C# An unexpected 'StartObject' node was found for property 'InputArguments' when reading from the JSON reader. A 'PrimitiveValue' node was expectedC# 从 JSON 阅读器读取时,为属性“InputArguments”找到了意外的“StartObject”节点。需要一个“PrimitiveValue”节点
【发布时间】:2019-03-03 13:22:16
【问题描述】:

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

“为名为的属性找到了意外的 'StartObject' 节点 从 JSON 阅读器读取时的“InputArguments”。一个“原始价值” 节点是预期的”

我在 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】:

    这似乎是一个仔细阅读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-\"}"
    

    看起来您将不得不“双重转义”这部分字符串,如下所示:

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

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

    【讨论】:

      猜你喜欢
      • 2019-10-21
      • 2017-08-29
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 2014-02-15
      • 1970-01-01
      相关资源
      最近更新 更多