json - 无法反序列化 Azure Rest API RoleDefinition 对象正文中的当前 JSON 对象 - PowerShell
问题描述
我正在创建RoleDefinition 更新函数并遇到请求正文的 JSON 格式问题。
这就是我尝试创建 JSON-Body 的方式:
$requestBody = [PSCustomObject]@{
properties = @{
assignableScopes = $RoleDefinition.properties.assignableScopes
description = $RoleDefinition.properties.description
permissions = @{
actions = $RoleDefinition.properties.permissions.actions
notActions = $RoleDefinition.properties.permissions.notActions
}
roleName = $RoleDefinition.properties.roleName
type = $RoleDefinition.properties.type
}
}
$body = ConvertTo-Json -InputObject $requestBody -Depth 3
JSON/ 结果看起来不错,但我收到“无法反序列化当前 JSON 对象”错误。特别是对于“properties.permissions.actions”对象。
{
"properties": {
"assignableScopes": [
"Foo",
"Bar",
"Baz"
],
"description": "My Role Description",
"roleName": "May Role Name",
"type": "CustomRole",
"permissions": {
"actions": [
"Foo",
"Bar",
"Baz"
],
"notActions": [
"Foo",
"Bar",
"Baz"
]
}
}
}
万一我忘记了什么,我已经添加了整个错误:
Invoke-RestMethod : {"error":{"code":"InvalidRequestContent","message":"The content of your request was not valid, and the original object could not be deserialized. Exception message:
'Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'Microsoft.Authorization.PASFE.Models.PASRP.AzurePermission[]' because the type requires a JSON array (e.g.
[1,2,3]) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a
primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to
deserialize from a JSON object.\r\nPath 'properties.permissions.actions', line 10, position 53.'"}}
At C:\Users\Foo\localGit\custom_roles\scripts\Test2.ps1:82 char:20
+ ... $Respond = Invoke-RestMethod -Method PUT -Uri $ApiUri -Headers $Toke ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
解决方案
西奥的重播让一切变得不同!感谢双方!
$requestBody = [PSCustomObject]@{
properties = @{
assignableScopes = $RoleDefinition.properties.assignableScopes
description = $RoleDefinition.properties.description
permissions = @(
@{
actions = @($RoleDefinition.properties.permissions.actions);
notActions = @($RoleDefinition.properties.permissions.notActions)
}
)
roleName = $RoleDefinition.properties.roleName
type = $RoleDefinition.properties.type
}
}
$body = ConvertTo-Json -InputObject $requestBody -Depth 4
推荐阅读
- javascript - React App 生产构建空白只有一个空白页面
- ios - ios 中的 react-native 构建失败
- python - 在python中以不同格式返回日期
- android - 活动未在紧密循环中更新
- reactjs - Reactjs 中的分页范围
- assembly - 如何在avr中将int8转换为int16
- node.js - Express JS 中的验证器错误 http POST 响应
- javascript - 如何替换子元素的类?
- vue.js - Nuxt.js:无法生成路由
- javascript - 是否有某种方法可以在不使用 .includes 或 indexOf(element) 函数的情况下搜索按钮单击过滤器?