Powershell ConvertFrom-Json 访问 json 值

Powershell ConvertFrom-Json accessing json value

我有一个 json 文件,我想将其转换为 csv。但是,实际值嵌套在我需要访问的 reading 键中。它 returns System.Object[] 我知道我必须访问它,但我不知道如何访问它。

以下是result.json文件,

{"totalNumberOfReadings":1488,
    "readings":
        [
           {"meterNumber":"xxxxx","date":"2022-02-01 00:00","reading":0.0,"status":"AsRequested"},
           {"meterNumber":"xxxxx","date":"2022-01-31 23:30","reading":0.0,"status":"As Requested"},
           {"meterNumber":"xxxxx","date":"2022-01-31 23:00","reading":0.0,"status":"As Requested"},
           {"meterNumber":"xxxxx","date":"2022-01-31 23:00","reading":0.0,"status":"As Requested"},
        ]
}

我的脚本是这样的

C:\> (Get-Content -Raw result.json | ConvertFrom-Json) | ConvertTo-Csv -NoTypeInformation

输出是这样的

"totalNumberOfReadings","readings"
"1488","System.Object[]"

这只是summary/metadata。我想要键值里面的实际内容,如何访问值?

要么使用会员权限:

(Get-Content -Raw result.json | ConvertFrom-Json).readings | ConvertTo-Csv -NoTypeInformation

...或仅管道命令:

Get-Content -Raw result.json | ConvertFrom-Json | ForEach-Object readings | ConvertTo-Csv -NoTypeInformation

ForEach-Object readingsForEach-Object -MemberName readingsshort form