Powershell选择NoteProperty类型对象从对象[到CSV和Access元素]

mftmpeh8  于 2023-05-26  发布在  Shell
关注(0)|答案(1)|浏览(136)

我引用了这个问题的答案,

我的样本数据

$personData = @'
{
  "persons": [{
      "johndoe": [{
          "id": "4334234 <>",
         
          "family_adress": {
            "mother": "address of family",
            "father": "",
            "brother": "address of family"
          },
          
          "childiren": {
            "first": 1024,
            "second":128
          },

          "years_activity": {
              "2021":  [ "sample entry-1", "sample entry-2" ],
              "2022":  [ "sample entry-1", "sample entry-2" ]
          }
     }],

      "johndoe2": [{
          "id": "4334234 <>",
         
          "family_adress": {
            "mother": "address of family...",
            "father": "",
            "brother": "...address of family"
          },
          
          "childiren": {
            "first": 25,
            "second": 12
          },

          "years_activity": {
              "2021":  [ "sample entry-4", "sample entry-r" ],
              "2022":  [ "sample entry-5", "sample entry-s" ]
          }
     }]

  }]
}
'@ | ConvertFrom-Json

我的输出:

NamePath                                      Value
--------                                      -----
persons[0].johndoe[0].id                      4334234 <>
persons[0].johndoe[0].family_adress.mother    address of family
persons[0].johndoe[0].family_adress.father
persons[0].johndoe[0].family_adress.brother   address of family
persons[0].johndoe[0].childiren.first         1024
persons[0].johndoe[0].childiren.second        128
persons[0].johndoe[0].years_activity.2021[0]  sample entry-1
persons[0].johndoe[0].years_activity.2021[1]  sample entry-2
persons[0].johndoe[0].years_activity.2022[0]  sample entry-1
persons[0].johndoe[0].years_activity.2022[1]  sample entry-2
persons[0].johndoe2[0].id                     4334234 <>
persons[0].johndoe2[0].family_adress.mother   address of family...
persons[0].johndoe2[0].family_adress.father
persons[0].johndoe2[0].family_adress.brother  ...address of family
persons[0].johndoe2[0].childiren.first        25
persons[0].johndoe2[0].childiren.second       12
persons[0].johndoe2[0].years_activity.2021[0] sample entry-4
persons[0].johndoe2[0].years_activity.2021[1] sample entry-r
persons[0].johndoe2[0].years_activity.2022[0] sample entry-5
persons[0].johndoe2[0].years_activity.2022[1] sample entry-s
  • 我想用CSV访问此NamePaths
    **标题:**id family_address children years_activitys //**我的行:**JohnDoe,JohnDoe 2
  • 在CSV之后,我想访问打印所有属性x,
foreach (person in CSV) {
  id: 
  family_address: 
  childrens: 
  years_activitys: (if header equal to SubArray also print them separately)
}
  • 我也可以用结构化的格式访问这些值,比如

foreach(activities in person.$username.years_activitys){ key:值}
我正在努力做这样的事情,提前感谢您的帮助。

jpfvwuh4

jpfvwuh41#

试着跟随。我用你的输入创建了一个表,这样我就可以测试。我把活动按日期分开列,而不是另起一行

$data = @"
persons[0].johndoe[0].id                      4334234 <>
persons[0].johndoe[0].family_adress.mother    address of family
persons[0].johndoe[0].family_adress.father
persons[0].johndoe[0].family_adress.brother   address of family
persons[0].johndoe[0].childiren.first         1024
persons[0].johndoe[0].childiren.second        128
persons[0].johndoe[0].years_activity.2021[0]  sample entry-1
persons[0].johndoe[0].years_activity.2021[1]  sample entry-2
persons[0].johndoe[0].years_activity.2022[0]  sample entry-1
persons[0].johndoe[0].years_activity.2022[1]  sample entry-2
persons[0].johndoe2[0].id                     4334234 <>
persons[0].johndoe2[0].family_adress.mother   address of family...
persons[0].johndoe2[0].family_adress.father
persons[0].johndoe2[0].family_adress.brother  ...address of family
persons[0].johndoe2[0].childiren.first        25
persons[0].johndoe2[0].childiren.second       12
persons[0].johndoe2[0].years_activity.2021[0] sample entry-4
persons[0].johndoe2[0].years_activity.2021[1] sample entry-r
persons[0].johndoe2[0].years_activity.2022[0] sample entry-5
persons[0].johndoe2[0].years_activity.2022[1] sample entry-s
"@

#Create Table for Testing to duplicate OPs input
$reader = [System.IO.StringReader]::new($data)
$table = [System.Collections.ArrayList]::new()
while(($line = $reader.ReadLine()) -ne $null)
{
   $splitArray = $line.Split(' ', [System.StringSplitOptions]::RemoveEmptyEntries)
   $newRow = [PSCustomObject]@{
      NamePath = $splitArray[0] 
      Value = $splitArray[1]
   }
   $table.Add($newRow) | Out-Null
}
$table

#start of real code
$people = [System.Collections.ArrayList]::new()
$name = ""
foreach($row in $table)
{
   $splitArray = $row.NamePath.Split('.')
   $newName = $splitArray[1]
   if($name -ne $newName)
   {
      $newRow = [PSCustomObject]@{
         Name = $newName 
         ID = $row.Value
      }
      $people.Add($newRow) | Out-Null
      $name = $newName
   }
   else
   {
      if($splitArray[2] -ne 'years_activity')
      {
         $pName = $splitArray[2] + '_' + $splitArray[3]
         $newRow | Add-Member -NotePropertyName $pName -NotePropertyValue $row.Value
      }
      else
      { 
         $newRow | Add-Member -NotePropertyName $splitArray[3] -NotePropertyValue $row.Value
      }
   }
}
$people
$people | export-csv 'c:\temp\test.csv'

相关问题