从 JArray 到 JObject 的转换无效
Invalid cast from JArray to JObject
我已经实现了与 Selenium -- Testrail 的集成。一切都是正确的,直到今天我不断收到:
OneTimeSetUp: System.InvalidCastException : Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray'.
这是失败的代码行:
JArray getListOfAllActiveTestRun = (JArray)client.SendGet($"get_runs/{ProjectId}");
因为我假设我需要一个 JArray
以便稍后在 foreach 循环中使用它(完整代码视图)
if (RunId == null)
{
JArray getListOfAllActiveTestRun = (JArray)client.SendGet($"get_runs/{ProjectId}");
foreach (JObject testRun in getListOfAllActiveTestRun)
{
bool isCompleted = Convert.ToBoolean(testRun["is_completed"]);
string lastTestRunId = testRun["id"].ToString();
string lastSuiteId = testRun["suite_id"].ToString();
int failedTestCount = testRun["failed_count"].Value<int>();
int untestedTestCount = testRun["untested_count"].Value<int>();
if (!isCompleted && lastSuiteId.Equals(SuitId) && failedTestCount > 0 || !isCompleted && lastSuiteId.Equals(SuitId) && untestedTestCount > 0) // we are checking that there is not finished testRun with suitId equal to this and failed tests and untested tests
{
RunId = lastTestRunId;
break;
}
}
}
检查了解决方案,大部分时间我都面临 JsonConvert.DeserializeObject
选项,但我不确定这对我来说是否是正确的提示。
编辑 (JSON)
"runs":
{
"id":2874,
"suite_id":878,
"name":"[ENV: TEST] [BACKOFFICE] Automation Test Run - [20:02:55 PM]",
"description":null,
"milestone_id":null,
"assignedto_id":null,
"include_all":true,
"is_completed":false,
"completed_on":null,
"config":null,
"config_ids":[
],
"passed_count":171,
"blocked_count":0,
"untested_count":1,
"retest_count":0,
"failed_count":3,
"custom_status1_count":0,
"custom_status2_count":0,
"custom_status3_count":0,
"custom_status4_count":0,
"custom_status5_count":0,
"custom_status6_count":0,
"custom_status7_count":0,
"project_id":19,
"plan_id":null,
"created_on":1631901776,
"updated_on":1631901776,
"refs":null,
"created_by":124,
"url":"ssss"
}
从今天开始我也遇到了同样的问题 -- TestRail 的 API 似乎发生了变化。
来自 TestRail 的 API 参考:“...这些批量端点将不再 return 所有实体的数组,而是 return 具有附加分页字段和最多 250 个实体的数组。"
来源:https://www.gurock.com/testrail/docs/api/reference/runs#getruns
我已经实现了与 Selenium -- Testrail 的集成。一切都是正确的,直到今天我不断收到:
OneTimeSetUp: System.InvalidCastException : Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray'.
这是失败的代码行:
JArray getListOfAllActiveTestRun = (JArray)client.SendGet($"get_runs/{ProjectId}");
因为我假设我需要一个 JArray
以便稍后在 foreach 循环中使用它(完整代码视图)
if (RunId == null)
{
JArray getListOfAllActiveTestRun = (JArray)client.SendGet($"get_runs/{ProjectId}");
foreach (JObject testRun in getListOfAllActiveTestRun)
{
bool isCompleted = Convert.ToBoolean(testRun["is_completed"]);
string lastTestRunId = testRun["id"].ToString();
string lastSuiteId = testRun["suite_id"].ToString();
int failedTestCount = testRun["failed_count"].Value<int>();
int untestedTestCount = testRun["untested_count"].Value<int>();
if (!isCompleted && lastSuiteId.Equals(SuitId) && failedTestCount > 0 || !isCompleted && lastSuiteId.Equals(SuitId) && untestedTestCount > 0) // we are checking that there is not finished testRun with suitId equal to this and failed tests and untested tests
{
RunId = lastTestRunId;
break;
}
}
}
检查了解决方案,大部分时间我都面临 JsonConvert.DeserializeObject
选项,但我不确定这对我来说是否是正确的提示。
编辑 (JSON)
"runs":
{
"id":2874,
"suite_id":878,
"name":"[ENV: TEST] [BACKOFFICE] Automation Test Run - [20:02:55 PM]",
"description":null,
"milestone_id":null,
"assignedto_id":null,
"include_all":true,
"is_completed":false,
"completed_on":null,
"config":null,
"config_ids":[
],
"passed_count":171,
"blocked_count":0,
"untested_count":1,
"retest_count":0,
"failed_count":3,
"custom_status1_count":0,
"custom_status2_count":0,
"custom_status3_count":0,
"custom_status4_count":0,
"custom_status5_count":0,
"custom_status6_count":0,
"custom_status7_count":0,
"project_id":19,
"plan_id":null,
"created_on":1631901776,
"updated_on":1631901776,
"refs":null,
"created_by":124,
"url":"ssss"
}
从今天开始我也遇到了同样的问题 -- TestRail 的 API 似乎发生了变化。
来自 TestRail 的 API 参考:“...这些批量端点将不再 return 所有实体的数组,而是 return 具有附加分页字段和最多 250 个实体的数组。"
来源:https://www.gurock.com/testrail/docs/api/reference/runs#getruns