无法从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,
}
}
]
}
]
}
想断言优先级和名称的详细信息,但无法做到.
Want to assert the details of priority and name but was not able to do it.
这是我已经实现的:
$Ndetails = templateProperties.parameters.Ns.value.SR
foreach ($Ndata in $Ndetails) {
$Ndata .properties.destinationPortRange |
Should -BeExactly @('1111','1111','1115')
}
如何在PowerShell中使用Pester解决相同的问题?
How to resolve the same using Pester in PowerShell?
推荐答案
您无需为此使用foreach
.您可以为此使用Select-Object
.假设您的JSON为注释中链接的 @Mark Wragg :
You don't need to use foreach
for this. You can just use Select-Object
for this. Assuming your JSON is as @Mark Wragg linked in the comments:
$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
您的测试文件应如下所示:
Your test file should look like this:
$result = $t.Ns.value.SR.properties.destinationPortRange
it 'destinationPortRange matches' {
$result | Should -BeExactly @('1111','1111','1115')
}
说明
您对foreach
的使用不正确,因为您比较了单个元素(还要注意,我删除了不必要的空间)
Explanation
Your use of foreach
was incorrect as you compared single element (also notice that I deleted unnecessary space)
$Ndata.properties.destinationPortRange
到数组
| Should -BeExactly @('1111','1111','1115')
您要做的是像我的示例中那样,将数组与数组进行比较.
What you have to do is to compare array to array as in my example.
这篇关于无法从JSON数据中获取单个详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!