我有一个JSON
文件,在其中我保持了很少的设置。以下是我的JSON文件
{
"1":{
"foundation_name":"Pre-Prod1",
"api_url": "https://sys1.com"
},
"2":{
"foundation_name":"Pre-Prod2",
"api_url": "https://sys-2.com"
},
"3":{
"foundation_name":"Prod1",
"api_url": "https://sys5.com"
}
}
我正在尝试通过迭代在屏幕上阅读此JSON
和PRINT
。以下是我的Powershell
脚本
Function FoundationSettings {
Write-Host 'Reading from File'
$foundations = Get-Content ".\foundations.json" -Raw | ConvertFrom-Json
Write-Host $foundations
return $foundations
}
Function DisplayFoundations {
$foundations = FoundationSettings
foreach ($foundation in $foundations) {
Write-Host 'here ..'
Write-Host $foundation.foundation_name
}
}
但是它只是以这种方式打印
Reading from File
@{1=; 2=; 3=}
here ..
如何解决?我需要解析JSON,并根据需要获得api_url
数据和foundation_name
答案 0 :(得分:1)
如果该格式不可更改,则可以使用:
frame.swaplevel(0, 1, 1).Green
Ohio Colorado
a 1 0 2
2 3 5
b 1 6 8
2 9 11
如果可以更改它,请使用JSON数组,其中array更合适:
$obj = @'
{
"1":{
"foundation_name":"Pre-Prod1",
"api_url": "https://sys1.com"
},
"2":{
"foundation_name":"Pre-Prod2",
"api_url": "https://sys-2.com"
},
"3":{
"foundation_name":"Prod1",
"api_url": "https://sys5.com"
}
}
'@ | ConvertFrom-Json
$listOfObjects = ($obj | Get-Member -MemberType NoteProperty).Name | % { $obj.$_ }
哪个给:
$listOfObjects | ConvertTo-Json
答案 1 :(得分:1)
访问基础属性的值:
Function DisplayFoundations {
$foundations = FoundationSettings
foreach ($foundation in $foundations.psobject.Properties.Value) {
Write-Host 'here ..'
Write-Host $foundation.foundation_name
}
}