【发布时间】:2021-01-18 09:36:24
【问题描述】:
我有用逗号分隔的用户输入,我正在使用 split 函数来获取不同的值。我有一个以 JSON 格式返回一些数据的 API。我想根据用户输入过滤来自 API Json 的数据
Powershell 代码
#Get Input data
$GetIds = Read-Host -Prompt 'Enter Ids:'
#Example 1,2
#If they enter 1,2, I want results data of John and Mark
#API Call Data
$json = @'
{
"results": [
{
"id": "1",
"name": "John",
},
{
"id": "2",
"name": "Mark",
},
{
"id": "3",
"name": "Rachel",
}
]
}
'@
$Obj = ConvertFrom-Json $json
#Split by comma
$userInputData = -split $GetIds
#Filter json with $userInputData
$FilteredData = $json | Where-Object { $_.id -eq #loop through $userInputData }
我希望过滤后的数据返回由 userInput 数据过滤的 $json。谢谢
【问题讨论】:
标签: json powershell powershell-core