无法从 JSON 数据中获取个人详细信息
Not able to fetch the individual details from JSON data
"Ns": {
"value": [
{
"Nname": "exa",
"SR": [
{
"name": "port1",
"properties": {
"description": "Allow port1",
"destinationPortRange": "1111",
"priority": 100
}
},
{
"name": "port1_0",
"properties": {
"description": "Allow port1",
"destinationPortRange": "1111",
"priority": 150
}
},
{
"name": "port2",
"properties": {
"description": "Allow 1115",
"destinationPortRange": "1115",
"priority": 100,
}
}
]
}
]
}
想要断言优先级和名称的详细信息,但无法做到。
这是我实现的:
$Ndetails = templateProperties.parameters.Ns.value.SR
foreach ($Ndata in $Ndetails) {
$Ndata .properties.destinationPortRange |
Should -BeExactly @('1111','1111','1115')
}
如何在 PowerShell 中使用 Pester 解决同样的问题?
您不需要为此使用 foreach
。您可以为此使用 Select-Object
。假设您的 JSON 与评论中链接的 @Mark Wragg 相同:
$Json = @'
[{
"Ns": {
"value": [{
"Nname": "exa",
"SR": [{
"name": "port1",
"properties": {
"description": "Allow port1",
"destinationPortRange": "1111",
"priority": 100
}
},
{
"name": "port1_0",
"properties": {
"description": "Allow port1",
"destinationPortRange": "1111",
"priority": 150
}
},
{
"name": "port2",
"properties": {
"description": "Allow 1115",
"destinationPortRange": "1115",
"priority": 100
}
}
]
}]
}
}]
'@
$t = $Json | ConvertFrom-Json
您的测试文件应如下所示:
$result = $t.Ns.value.SR.properties.destinationPortRange
it 'destinationPortRange matches' {
$result | Should -BeExactly @('1111','1111','1115')
}
说明
你对 foreach
的使用不正确,因为你比较了单个元素(另请注意,我删除了不必要的 space)
$Ndata.properties.destinationPortRange
到数组
| Should -BeExactly @('1111','1111','1115')
你需要做的就是像我的例子一样比较数组。
"Ns": {
"value": [
{
"Nname": "exa",
"SR": [
{
"name": "port1",
"properties": {
"description": "Allow port1",
"destinationPortRange": "1111",
"priority": 100
}
},
{
"name": "port1_0",
"properties": {
"description": "Allow port1",
"destinationPortRange": "1111",
"priority": 150
}
},
{
"name": "port2",
"properties": {
"description": "Allow 1115",
"destinationPortRange": "1115",
"priority": 100,
}
}
]
}
]
}
想要断言优先级和名称的详细信息,但无法做到。
这是我实现的:
$Ndetails = templateProperties.parameters.Ns.value.SR
foreach ($Ndata in $Ndetails) {
$Ndata .properties.destinationPortRange |
Should -BeExactly @('1111','1111','1115')
}
如何在 PowerShell 中使用 Pester 解决同样的问题?
您不需要为此使用 foreach
。您可以为此使用 Select-Object
。假设您的 JSON 与评论中链接的 @Mark Wragg 相同:
$Json = @'
[{
"Ns": {
"value": [{
"Nname": "exa",
"SR": [{
"name": "port1",
"properties": {
"description": "Allow port1",
"destinationPortRange": "1111",
"priority": 100
}
},
{
"name": "port1_0",
"properties": {
"description": "Allow port1",
"destinationPortRange": "1111",
"priority": 150
}
},
{
"name": "port2",
"properties": {
"description": "Allow 1115",
"destinationPortRange": "1115",
"priority": 100
}
}
]
}]
}
}]
'@
$t = $Json | ConvertFrom-Json
您的测试文件应如下所示:
$result = $t.Ns.value.SR.properties.destinationPortRange
it 'destinationPortRange matches' {
$result | Should -BeExactly @('1111','1111','1115')
}
说明
你对 foreach
的使用不正确,因为你比较了单个元素(另请注意,我删除了不必要的 space)
$Ndata.properties.destinationPortRange
到数组
| Should -BeExactly @('1111','1111','1115')
你需要做的就是像我的例子一样比较数组。