迭代 Json 属性和 PSCustom 对象

问题描述 投票:0回答:1

我正在研究从 json 文件中迭代 json 属性和值,而不需要先了解模式。我的代码可以处理我测试过的许多 json 文件或下载,但在迭代嵌套值时会绘制空白。我看过各种使用 Get-Member 和 .psobject.name 和 NoteProperty 的帖子...但所有帖子似乎都对 json 内容有先验知识。我测试过这个

{
  "FoodChoices": [
    {
      "id": 1,
      "name": "TakeOut",
      "variableGroups": [],
      "variables": {
        "Location": {
          "value": "Fast Food"
        },
        "Beverage": {
          "value": "Soda or Lemondade"
        },
        "Tip": {
          "value": "No Way"
        }
      }
    },
    {
      "id": 2,
      "name": "Gourmet",
      "variableGroups": [],
      "variables": {
        "Location": {
          "value": "Nice Resturant"
        },
        "Beverage": {
          "value": "Dirty Martini"
        },
        "Tip": {
          "value": "Maybe"
        }
      }
    }
  ]
}

但不得不拼凑迭代

$jfile = "$PSScriptRoot\FoodChoice.json"

$file = Get-Content $jFile -Raw
$parse = ($file.substring(0,1) -ne "[")
$json = $file | ConvertFrom-Json

$parent = $json | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name
if ($parse -eq $true)
{
   foreach ($p in $parent)
   {
      $d = $json.$p
      if ($d.length -gt 1) { $data = $d }
   }
}
else {$data = $json }
$out = "Key" + "`t" + "Value" + "`n"
$keys = $data | get-member -type properties | Foreach-Object name
foreach ($d in $data)
{
   foreach ($k in $keys)
   {
       $name = $d.$k
       if ($name -match "@")
       {
           $out += "$k`t`n"
           $members = Get-Member -InputObject $name
           foreach ($m in $members)
           {
              if ($m -match "System.Management.Automation.PSCustomObject")
              {
                 $m1 = $m.ToString()
                 $m1 = $m1.Replace("System.Management.Automation.PSCustomObject","") 
                 if ($m1 -match "@")
                 {
                    $m1 = $m1.Replace("@{","")
                    $m1 = $m1.Replace("}","")
                    $m1 = $m1 -split "="
                    $m2 = $m1[0]
                    $m3 = $m1[2]
                    $out += "$m2`t$m3`n"
                 }
                 else {$out += "$name`t$m1`n"}
              }
           }
       }
       else {$out += "$k`t$name`n"}
   }
}
$out
Exit

希望有一种通用方法来解析 System.Management.Automation.PSCustomObject 类型的嵌套属性。

脚本输出

Key Value
id  1
name    TakeOut
variableGroups  
variables   
 Beverage   Soda or Lemondade
 Location   Fast Food
 Tip    No Way
id  2
name    Gourmet
variableGroups  
variables   
 Beverage   Dirty Martini
 Location   Nice Resturant
 Tip    Maybe
json powershell pscustomobject
1个回答
0
投票

我在处理 json 时没有发现任何问题。不过我认为在 powershell 中处理键/值 JSON 的主要方法是使用

convertFrom-Json

$jsondata = Get-Content D:\Scripts\jason_sample.txt

$jsondata | ConvertFrom-Json #Should give you all the child underneath and you can expand also. 

($jsondata | ConvertFrom-Json).FoodChoices ## should give you all the id, name , etc 

如果您仍然在努力解决这个问题,那么最初您可以使用

 [System.Text.RegularExpressions.Regex]::Unescape($_)

来解决这个问题
$jsondata | ConvertTo-Json | % { [System.Text.RegularExpressions.Regex]::Unescape($_) } | Out-File "File.json" -Force
© www.soinside.com 2019 - 2024. All rights reserved.