【发布时间】:2020-10-05 00:18:20
【问题描述】:
在 Azure DevOps 发布管道中使用 Powershell,我正在尝试将参数转换为 json 格式,然后再发布到 asp netcore 端点。其中一个参数是日期时间。
我收到以下 DateTime 类型的错误:
"errors":{"$.CreatedDate":["The JSON value could not be converted to System.Nullable`1[System.DateTime].
这是 Powershell 脚本。变量$(RELEASE.DEPLOYMENT.STARTTIME)是DevOps变量,输出日期格式2020-06-15 10:00:46Z
$params = @{
Name = "Test"
CreatedDate = $(RELEASE.DEPLOYMENT.STARTTIME)
}
Invoke-WebRequest -Uri https://mynetcoreendpoint -Method POST -Body ($params | ConvertTo-Json) -ContentType "application/json" -UseBasicParsing
json 在端点侧进行评估。这是 NetCore 端点
[HttpPost]
public ActionResult<ReleaseDTO> CreateRelease(ReleaseDTO release)
{
// Do some stuff
}
// Where ReleaseDTO has the property
public DateTime? CreatedDate { get; set; }
【问题讨论】:
-
你希望你的约会通过什么?作为你发布的字符串或整数(unix时间戳)或其他东西?
-
但是错误不是由提供的代码抛出的,对吗?在评估收到的 JSON 时发生错误。你能发布评估代码吗?
-
@doorman 当
$(RELEASE.DEPLOYMENT.STARTTIME)被扩展时,它是否包括封闭引号?也许您需要自己提供这些报价。 -
@doorman 如果在 NetCore 代码中将
CreatedDate替换为DateTime.Parse(CreatedDate, System.Globalization.CultureInfo.InvariantCulture)会发生什么? -
doorman:如果扩展的宏值没有用引号括起来并且您自己没有提供它们,
$params = ...语句将中断。[datetime] '2020-06-15 10:00:46Z'之类的演员 确实 工作,所以也许你的价值有隐藏的字符? @Thomas 的隐含问题也值得探讨——隐式 JSON 反序列化层期望什么格式?我已取消删除我的答案,并添加了一些通用指针。
标签: json powershell azure-devops