如何显示已从JSON转换的完整PSCustomObject

时间:2018-09-20 05:04:24

标签: powershell

我有一个需要使用PowerShell进行操作的JSON文件。我将其转换为PSCustomObject:

set vItems = objSourceFolder.Items
For intCount = vItems.Count To 1 Step -1
  Set objVariant = vItems.Item(intCount)
  ...
  set objVariant = Nothing
next

当我尝试通过以下方式打印$template = Get-Content $filePath -raw | ConvertFrom-Json 时:

$template

我得到:

Write-Host $template

我想看到@{$parameters=; variables=; resources=System.Object[]} parametersvariables内还有更多键值。如何打印出整个扩展文件?

此外,如何打印出特定键的值?例如,如果我只想打印出resources,它本身可能包含更多的键值对。

1 个答案:

答案 0 :(得分:0)

示例

在我的回答中,我使用的是Json example(如下所示),因为您的问题中没有示例:

$template = ConvertFrom-Json @'
{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}
'@

格式化复杂对象

正如您在此处看到的那样,原始Json文件实际上已经很好地表示了对象层次结构。这显然是Json(JavaScript)格式。 如果要将其显示为可逆的 PowerShell表达式,则可能要使用ConvertTo-Expression中的PowerShell函数PowerShell Gallery
用法示例:

Write-Host ($template | ConvertTo-Expression)

[PSObject]@{'glossary' = [PSObject]@{
                'GlossDiv' = [PSObject]@{
                        'GlossList' = [PSObject]@{'GlossEntry' = [PSObject]@{
                                        'Abbrev' = 'ISO 8879:1986'
                                        'Acronym' = 'SGML'
                                        'GlossDef' = [PSObject]@{
                                                'GlossSeeAlso' = @(
                                                        'GML',
                                                        'XML'
                                                )
                                                'para' = 'A meta-markup language, used to create markup languages such as DocBook.'
                                        }
                                        'GlossSee' = 'markup'
                                        'GlossTerm' = 'Standard Generalized Markup Language'
                                        'ID' = 'SGML'
                                        'SortAs' = 'SGML'
                                }}
                        'title' = 'S'
                }
                'title' = 'example glossary'
        }}

请注意,您可以使用PowerShell轻松反转表达式。含义:
$Expression = $template | ConvertTo-Expression # Object to Expression
$template = &$Expression # Expression back to Object

访问Json对象属性和数组项

考虑打印特定密钥:

要引用对象属性:

$template.Glossary.Title
example glossary

要引用数组项(在对象链中):

$template.Glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso[1]
XML