无法使用jsonpath检索嵌套属性

时间:2019-01-10 18:44:50

标签: jsonpath

使用jsonpath,我想检索嵌套的json属性,但无法这样做。查询搜索虽然没有返回任何值

 public List<Step> GetBuildSteps()
    {             
     queryString = string.Format("$..steps[?(@.task == '{0}' &&  @.inputs.inlineScript =~ /^.*{1}.*$/)]", "PowerShell@1", "BUILD_BUILDNUMBER");

        return queryJson(queryString);
    }

  private List<Step> queryJson(String queryString){
        var values = json.SelectTokens(queryString).ToList();

        return JsonConvert.DeserializeObject<List<Step>>(JsonConvert.SerializeObject(values, Formatting.Indented,
        new JsonSerializerSettings
        {
            NullValueHandling = NullValueHandling.Ignore
        }));
    }

 Here's the json: 
   {  
    "steps":[  
     {  
     "task":"NodeTool@0",
     "displayName":"Use Node 8.x",
     "inputs":{  
        "versionSpec":"8.x"
     }
    },
    {  
     "task":"Npm@0",
     "displayName":"npm install tfx globally",
     "inputs":{  
        "arguments":"npm i -g tfx-cli  ",
        "cwd":"SelfServeAdminExtn"
     }
    },
    {  
     "task":"Npm@1",
     "displayName":"Install packages",
     "inputs":{  

     }
    },
    {  
     "task":"PowerShell@1",
     "displayName":"Preprod - PowerShell Script",
     "inputs":{  
        "scriptType":"inlineScript",
        "inlineScript":"Write-Host \"Setting package version to \" + $Env:BUILD_BUILDNUMBER\n\n$pathToJson = \"SelfServeAdminExtn\\vss-extension-preprod.json\"\n$a = Get-Content $pathToJson | ConvertFrom-Json\n$a.version = $Env:BUILD_BUILDNUMBER\n$a | ConvertTo-Json -Depth 10 | set-content $pathToJson\n"
     }
    },
    {  
     "task":"CmdLine@1",
     "displayName":"Preprod - Copy config",
     "inputs":{  
        "arguments":"$(build.sourcesDirectory)\\SelfServeAdminExtn\\scripts\\config-preprod.ts $(build.sourcesDirectory)\\SelfServeAdminExtn\\scripts\\config.ts /y"
     }
    }
  ]
}

结果查询为:$ .. steps [?(@。task =='PowerShell @ 1'&& @ .inputs.inlineScript =〜/^.BUILD_BUILDNUMBER.$/)] 应该会产生一个结果,但是在这种情况下,不是这样

0 个答案:

没有答案