需要在文本文件中搜索字符串,并捕获位于第一个字符串之后的第二个字符串

时间:2019-07-15 20:18:02

标签: json powershell

我正在使用API​​调用,我需要在PowerShell中解析API返回的JSON。我正在搜索一个特定的名称,然后需要捕获其后的另一个项目。

$json = @"
{
    "Test": [
        {
            "association": "(None)",
            "description": "",
            "displayName": "BusIntel48.png",
            "galleryImage": "[PlugIn]Images;Trebuchet.PlugIn.Images.Images.Common.BusIntelligence.BusIntel48.png",
            "id": "944f73cc5c1a812eaa2ade43bd9ca0c9a40f963c56",
            "links": [],
            "localizedScopeName": "Built in",
            "name": "BusIntel48.png",
            "parentFolder": "ImagesCommonBusIntelligence",
            "parentIsScopeFolder": false,
            "scope": "FromResource",
            "scopeOwner": "",
            "standInKey": "DefType:ImageDef#Scope:FromResource#Id:944f73cc5c1a812eaa2ade43bd9ca0c9a40f963c56"
        },
        {
            "association": "(None)",
            "description": "",
            "displayName": "BusProcActOnEvent16.png",
            "galleryImage": "[PlugIn]Images;Trebuchet.PlugIn.Images.Images.Common.BusIntelligence.BusProcActOnEvent16.png",
            "id": "944f73cc5cb33339be6db84987baef3f7190c931cf",
            "links": [],
            "localizedScopeName": "Built in",
            "name": "BusProcActOnEvent16.png",
            "parentFolder": "ImagesCommonBusIntelligence",
            "parentIsScopeFolder": false,
            "scope": "FromResource",
            "scopeOwner": "",
            "standInKey": "DefType:ImageDef#Scope:FromResource#Id:944f73cc5cb33339be6db84987baef3f7190c931cf"
        }
    ]
}
"@

$x = $json | ConvertFrom-Json

$name = $x.Test | where { $_.name -eq "BusIntel48.png" }

我需要搜索图像文件的名称(例如“名称”:“ BusIntel48.png”),然后在“ standInKey:”之后捕获字符串:(例如DefType:ImageDef#Scope:FromResource#Id:944f73cc5c1a812eaa2ade43bd9ca0c9a40f963c56 )。

1 个答案:

答案 0 :(得分:0)

只需要更改最后一行。

您还可以使用来选择一个属性:

$name = $x.Test | where { $_.name -eq "BusIntel48.png" } | Select-Object -Property "standInKey"