【问题标题】:PowerShell and exif data in Json formatJson 格式的 PowerShell 和 exif 数据
【发布时间】:2020-07-24 08:51:59
【问题描述】:

我目前正在尝试学习 powershell。我有一些代码通过一些 .jpg 图像,将这些图像发送到 exiftool 并将它们的 exif 信息输出到 json 文件。然后我使用 Get-Content 将 Json 文件带入 powershell,然后使用 ConvertFrom-Json 将其转换为 PSObject。我无法理解如何从 PSObject 访问我需要的数据。例如,我想获取图像的文件名及其曝光设置。我试过 psobject.properties.Name ,它只显示 PSObject 的属性,而不是其中的实际对象。然后我尝试了 Get-Member 及其一些属性,但无济于事。我相信 Get-Member 是我需要走的路线,但我不完全确定如何使用它。

这是一个非常简洁的Json示例:

[
    "[{",
    "  \"FileName\": \"DJI_0001.JPG\",",
    "  \"ExposureTime\": 0.005,",
    "},",
    "{",
    "  \"FileName\": \"DJI_0002.JPG\",",
    "  \"ExposureTime\": 0.003125,",
    "},",
    "{",
    "  \"FileName\": \"DJI_0004.JPG\",",
    "  \"ExposureTime\": 0.00625,",
    "}]"
]

这是我尝试过的一些代码:

$scriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent

Write-Host "Procesing Files..."

$command = $scriptDir + '\exiftool.exe ' + $directory + ' -json -n -r -EXT JPG'

$allmetadata = Invoke-Expression $command

#Write-Host $allmetadata

$allmetadata | ConvertTo-Json | Out-File "$scriptDir\JsonOut.json" 

#########################################################################################################
#
# This Gets the Json File and converts it to a PSObject 
#
#########################################################################################################

$convertedmeta = Get-Content "$scriptDir/JsonOut.json" -Raw -Encoding UTF8 | ConvertFrom-Json

Write-Host $convertedmeta

#########################################################################################################
#
# This is code where I was trying to explore the information within the PSObject trying to figure out 
# where the FileName and ExpsoureTime are kept so I can call the values
#
#########################################################################################################

Write-Host "Exploring JSON File"
#$jsonInfoExplore = $convertedmeta.PsObject.Properties.Value.PsObject.Properties.Name
#$jsonInfoExplore = $convertedmeta.psobject.properties.Name
#$jsonInfoExplore = $convertedmeta.psobject.properties.Value
$jsonInfoExplore = $convertedmeta | Get-Member 

Write-Host $jsonInfoExplore

正如您所看到的代码被注释掉的地方,我尝试了几种不同的方法来获取我正在寻找的信息。我只是不完全了解如何从 PSObject 调用数据。

我们将不胜感激任何帮助和指导。

【问题讨论】:

    标签: json powershell powershell-4.0 exiftool


    【解决方案1】:

    那个 json 看起来很奇怪,好像所有东西都被转义了。您必须 两次 转换 json。

    cat file.json | convertfrom-json | convertfrom-json
    
    FileName     ExposureTime
    --------     ------------
    DJI_0001.JPG        0.005
    DJI_0002.JPG     0.003125
    DJI_0004.JPG      0.00625
    

    它应该看起来像这样,一个由三个对象组成的数组:

    [{
      "FileName": "DJI_0001.JPG",
      "ExposureTime": 0.005,
    },
    {
      "FileName": "DJI_0002.JPG",
      "ExposureTime": 0.003125,
    },
    {
      "FileName": "DJI_0004.JPG",
      "ExposureTime": 0.00625,
    }]
    

    【讨论】:

    • 虽然我对 PS 不太熟悉,但在我看来,代码采用了 exiftool 的输出,由于-json 选项,该输出已经在 json 中,并再次将其转换为 json ?特别是$allmetadata | ConvertTo-Json | Out-File "$scriptDir\JsonOut.json"
    • @StarGeek 这可以解释。
    • 我什至没有意识到我对 Json 做了什么。我还认为 Json 看起来很奇怪。我什至用 JsonLint 验证了它。我将 $allmetadata 更改为 $allmetadata | Out-File "$scriptDir\JsonOut.json" -Encoding UTF-8 json文件现在看起来好多了,所以我打算花点时间玩一下,看看能不能得到我想要的。
    猜你喜欢
    • 2014-08-24
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    相关资源
    最近更新 更多