代码之家  ›  专栏  ›  技术社区  ›  Dmitrij Kultasev

从PSCustomObject类型的数组中获取值

  •  0
  • Dmitrij Kultasev  · 技术社区  · 6 年前

    PSCustomObject 找不到正确的路。

    PS: $val
      entry1 : @{order=10; isConditionalDeploy=1; isDropExtendedProperties=0}
      entry2 : @{order=20; isConditionalDeploy=1; isDropExtendedProperties=0}
      entry3 : @{order=30; isConditionalDeploy=1; isDropExtendedProperties=0}
    
    PS: $val.GetType()
      IsPublic IsSerial Name     BaseType
      -------- -------- ----     --------
      True     True     Object[] System.Array
    
    PS: $val[0]
      entry1 : @{order=10; isConditionalDeploy=1; isDropExtendedProperties=0}
      entry2 : @{order=20; isConditionalDeploy=1; isDropExtendedProperties=0}
      entry3 : @{order=30; isConditionalDeploy=1; isDropExtendedProperties=0}
    
    PS: $val[0].GetType()
      IsPublic IsSerial Name           BaseType
      -------- -------- ----           --------
      True     False    PSCustomObject System.Object
    

    我试了这么多方法,结果总是一样的。我试着从 $val.PSObject 但运气不好

    $val | gm
    Name              MemberType   Definition
    ----              ----------   ----------
    Equals            Method       bool Equals(System.Object obj)
    GetHashCode       Method       int GetHashCode()
    GetType           Method       type GetType()
    ToString          Method       string ToString()
    entry1            NoteProperty System.Management.Automation.PSCustomObject entry1=@{order=10; isConditionalDeploy=1;
    entry2            NoteProperty System.Management.Automation.PSCustomObject entry2=@{order=20; isConditionalDeploy=1; 
    entry3            NoteProperty System.Management.Automation.PSCustomObject entry3=@{order=30; isConditionalDeploy=1; 
    
    0 回复  |  直到 6 年前
        1
  •  2
  •   Ben Richards    6 年前

    如果您只是尝试访问这些值,则应该可以:

    $json = '{
    "entry1":
        {
            "order":"10",
            "isConditionalDeploy":"1",
            "isDropExtendedProperties":"0"
        },
    "entry2":
        {
            "order":"20",
            "isConditionalDeploy":"1",
            "isDropExtendedProperties":"0"
        }
    }'
    
    $val = $json | ConvertFrom-Json
    
    $val | ForEach-Object {
        $_.PSObject.Properties.Value
    }
    

    输出

    order isConditionalDeploy isDropExtendedProperties
    ----- ------------------- ------------------------
    10    1                   0
    20    1                   0
    
        2
  •  1
  •   AdminOfThings    6 年前

    $val.psobject.properties.name
    

    要列出所有属性值,请运行以下命令:

    $val.psobject.properties.value
    

    $val.psobject.properties.name $值.psobject.properties.名称 $val.psobject.properties.name[0] $val.psobject.properties.value[0] .

    如果您已经知道属性名并且只需要值,那么可以按照Theo的建议访问值。

    {
    "entry1":
        {
            "order":"10",
            "isConditionalDeploy":"1",
            "isDropExtendedProperties":"0"
        },
    "entry2":
        {
            "order":"20",
            "isConditionalDeploy":"1",
            "isDropExtendedProperties":"0"
        }
    }
    

    $val

    $val = Get-Content json.json | ConvertFrom-Json
    $val | fl
    
    entry1 : @{order=10; isConditionalDeploy=1; isDropExtendedProperties=0}
    entry2 : @{order=20; isConditionalDeploy=1; isDropExtendedProperties=0}
    
    $val | gm
    
       TypeName: System.Management.Automation.PSCustomObject
    
       TypeName: System.Management.Automation.PSCustomObject
    
        Name        MemberType   Definition
        ----        ----------   ----------
        Equals      Method       bool Equals(System.Object obj)
        GetHashCode Method       int GetHashCode()
        GetType     Method       type GetType()
        ToString    Method       string ToString()
        entry1      NoteProperty Object[] entry1=System.Object[]
        entry2      NoteProperty Object[] entry2=System.Object[]
    

    完成以上所有操作后,我的属性名和值检索就如我所建议的那样工作。你能提供你的JSON文件内容吗?

        3
  •  0
  •   Walter Mitty    6 年前