我正在尝试将数据转换为JSON作为REST API的输入。我面临的挑战是,数据应该由多个深度组成(因为没有更好的词语)。我现在使用的代码是:
(@{name = "Contoso"; all_assets = "false"; all_users="false"; rules= @{type="fqdn"; operator="match"; terms=@("contoso") } }| ConvertTo-Json)现在的输出是:
{
"all_users": "false",
"name": "Contoso",
"all_assets": "false",
"rules": {
"operator": "match",
"terms": [
"contoso"
],
"type": "fqdn"
}
}REST-Api抱怨数据包含无效字符。查看输出,"rules:“部分包含{},而不是。我试过各种花招,但我似乎想不出这一招。
有人知道我哪里做错了吗?
发布于 2019-11-21 02:29:14
不管它值多少钱...
不是Unexpected ConvertTo-Json results? Answer: it has a default -Depth of 2问题的答案,而是通常如何使用ConvertTo-Expression cmdlet从Json文件构建PowerShell表达式:
'{
"all_users": "false",
"name": "Contoso",
"all_assets": "false",
"rules": [
{
"operator": "match",
"terms": [
"contoso"
],
"type": "fqdn"
}
]
}' | ConvertFrom-Json | ConvertTo-Expression
[pscustomobject]@{
'all_users' = 'false'
'name' = 'Contoso'
'all_assets' = 'false'
'rules' = ,[pscustomobject]@{
'operator' = 'match'
'terms' = ,'contoso'
'type' = 'fqdn'
}
}https://stackoverflow.com/questions/58954584
复制相似问题