无法反序列化 Azure Rest API RoleDefinition 对象主体中的当前 JSON 对象 - PowerShell
Cannot deserialize the current JSON object in Azure Rest API RoleDefinition Object Body - PowerShell
我正在创建 RoleDefinition Update Function and run into an JSON Format issue for the Request Body。
这就是我尝试创建 JSON-正文的方式:
$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
我正在创建 RoleDefinition Update Function and run into an JSON Format issue for the Request Body。
这就是我尝试创建 JSON-正文的方式:
$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