如何使用 PowerShell 从 json 响应中获取单个值
How to get a single value from a json response using PowerShell
我有以下json。我想从下面的响应中仅提取值 41 from "id": "41" 。假设我不知道该值是什么,我如何提取该单个值,将其分配给一个变量并查看输出?
{
"page": {
"offset": 0,
"total": 36,
"totalFilter": 1
},
"list": [
{
"id": "41",
"type": "ATBR",
"hostName": "AAMS",
"userId": "",
"userName": "",
"status": "CONNECTED",
"poolName": "",
"fullyQualifiedHostName": "-",
"updatedBy": "mscr",
"updatedOn": "2020-06-24T23:28:11.239894Z",
"botAgentVersion": "9.0"
}
]
}
到目前为止我有以下内容,但我不知道如何只获取单个值
$json = @"
{
"page": {
"offset": 0,
"total": 36,
"totalFilter": 1
},
"list": [
{
"id": "41",
"type": "ATBR",
"hostName": "AAMS",
"userId": "",
"userName": "",
"status": "CONNECTED",
"poolName": "",
"fullyQualifiedHostName": "-",
"updatedBy": "mscr",
"updatedOn": "2020-06-24T23:28:11.239894Z",
"botAgentVersion": "9.0"
}
]
}
"@
$x = $json | ConvertFrom-Json
$x.list[0]
$id = $x.list | Where { $_.list -eq "id" }
我当前的输出结果如下。我只想从中提取41。
id : 41
type : ATBR
hostName : AAMS
userId :
userName :
status : CONNECTED
poolName :
fullyQualifiedHostName : -
updatedBy : mscr
updatedOn : 2020-06-24T23:28:11.239894Z
botAgentVersion : 9.0
非常感谢任何帮助 - 提前致谢
将 JSON 转换为对象后,您可以使用 Where-Object
或 Where
过滤包含 id
的 list
数组元素具有非空和非空值。然后使用成员访问运算符 .
检索 id
属性.
的值
$json = @"
{
"page": {
"offset": 0,
"total": 36,
"totalFilter": 1
},
"list": [
{
"id": "41",
"type": "ATBR",
"hostName": "AAMS",
"userId": "",
"userName": "",
"status": "CONNECTED",
"poolName": "",
"fullyQualifiedHostName": "-",
"updatedBy": "mscr",
"updatedOn": "2020-06-24T23:28:11.239894Z",
"botAgentVersion": "9.0"
}
]
}
"@
$x = $json | ConvertFrom-Json
$id = ($x.list | Where id).id
使用 Where id
基本上检查是否 [boo]$x.list.id
returns true
。因此,如果您要使用 Where userId
,它将 return 什么都没有,因为 [bool]$x.list.userId
的计算结果为 false
。
您也可以使用 Select-Object
:
检索 id
值
$id = $x.list | Where id | Select-Object -Expand id
请注意,如果 list
中有多个对象(因为它是一个数组)包含 id
和一个值,多个 id
值将是 returned.
我有以下json。我想从下面的响应中仅提取值 41 from "id": "41" 。假设我不知道该值是什么,我如何提取该单个值,将其分配给一个变量并查看输出?
{
"page": {
"offset": 0,
"total": 36,
"totalFilter": 1
},
"list": [
{
"id": "41",
"type": "ATBR",
"hostName": "AAMS",
"userId": "",
"userName": "",
"status": "CONNECTED",
"poolName": "",
"fullyQualifiedHostName": "-",
"updatedBy": "mscr",
"updatedOn": "2020-06-24T23:28:11.239894Z",
"botAgentVersion": "9.0"
}
]
}
到目前为止我有以下内容,但我不知道如何只获取单个值
$json = @"
{
"page": {
"offset": 0,
"total": 36,
"totalFilter": 1
},
"list": [
{
"id": "41",
"type": "ATBR",
"hostName": "AAMS",
"userId": "",
"userName": "",
"status": "CONNECTED",
"poolName": "",
"fullyQualifiedHostName": "-",
"updatedBy": "mscr",
"updatedOn": "2020-06-24T23:28:11.239894Z",
"botAgentVersion": "9.0"
}
]
}
"@
$x = $json | ConvertFrom-Json
$x.list[0]
$id = $x.list | Where { $_.list -eq "id" }
我当前的输出结果如下。我只想从中提取41。
id : 41
type : ATBR
hostName : AAMS
userId :
userName :
status : CONNECTED
poolName :
fullyQualifiedHostName : -
updatedBy : mscr
updatedOn : 2020-06-24T23:28:11.239894Z
botAgentVersion : 9.0
非常感谢任何帮助 - 提前致谢
将 JSON 转换为对象后,您可以使用 Where-Object
或 Where
过滤包含 id
的 list
数组元素具有非空和非空值。然后使用成员访问运算符 .
检索 id
属性.
$json = @"
{
"page": {
"offset": 0,
"total": 36,
"totalFilter": 1
},
"list": [
{
"id": "41",
"type": "ATBR",
"hostName": "AAMS",
"userId": "",
"userName": "",
"status": "CONNECTED",
"poolName": "",
"fullyQualifiedHostName": "-",
"updatedBy": "mscr",
"updatedOn": "2020-06-24T23:28:11.239894Z",
"botAgentVersion": "9.0"
}
]
}
"@
$x = $json | ConvertFrom-Json
$id = ($x.list | Where id).id
使用 Where id
基本上检查是否 [boo]$x.list.id
returns true
。因此,如果您要使用 Where userId
,它将 return 什么都没有,因为 [bool]$x.list.userId
的计算结果为 false
。
您也可以使用 Select-Object
:
id
值
$id = $x.list | Where id | Select-Object -Expand id
请注意,如果 list
中有多个对象(因为它是一个数组)包含 id
和一个值,多个 id
值将是 returned.