![](/img/trans.png)
[英]Get single JSON property value from response JSON using Retrofit 2
[英]How to get a single value from a json response using PowerShell
我有以下 json。 我只想從下面的響應中提取“id”:“41”中的值 41。 假設我不知道該值是什么,我如何提取該單個值,將其分配給變量並查看 output?
{
"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" }
我當前的 output 結果如下。 我只想從中提取 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 轉換為 object 后,您可以使用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
返回true
。 因此,如果您要使用Where userId
,它不會返回任何內容,因為[bool]$x.list.userId
計算結果為false
。
您也可以使用Select-Object
檢索id
值:
$id = $x.list | Where id | Select-Object -Expand id
請注意,如果list
中有多個 object (因為它是一個數組)包含一個值的id
,則會返回多個id
值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.