试图在jsonpath查询中使用两个&&语句,但是我无法获得想要执行的逻辑
public List<Step> GetBuildSteps()
{
var rule1 = "Npm@1";
var rule2 = "npm build";
var rule3 = "Npm@0";
var rule4 = "Build";
var queryString = string.Format("$..steps[?(@.task == '{0}' &&
@.displayName =~ /^.*{1}.*$/ || @.task == '{2}' && @.displayName =~
/^.*{3}.*$/) ]", rule1, rule2, rule3, rule4);
return queryJson(queryString);
}
private List<Step> queryJson(String queryString)
{
var values = json.SelectTokens(queryString).ToList();
Console.WriteLine(queryString);
Console.WriteLine(JsonConvert.SerializeObject(values,
Formatting.Indented));
return
JsonConvert.DeserializeObject<List<Step>
(JsonConvert.SerializeObject(values, Formatting.Indented));
}
这是我的JSON示例:
{
"steps":[
{
"task":"Npm@1",
"displayName":"Dev - npm build",
"inputs":{
"filename":"tsc",
"arguments":"-p $(build.sourcesDirectory)\\SelfServeAdminExtn"
},
"enabled":"false",
"continueOnError":"true"
},
{
"task":"Npm@1",
"displayName":"Install Packages",
"inputs":{
"filename":"tfx"
},
"enabled":"false",
"condition":"succeededOrFailed()"
},
{
"task":"Npm@0",
"displayName":"Preprod - Build",
"inputs":{
"filename":"copy",
"arguments":"$(build.sourcesDirectory)\\ config.ts / y"
}
}
]
}
我希望语句?(condition1 && condition2 || condition3 && condition4)将起作用,但不会起作用。仅当我执行条件(condition1 && condition2)时它才有效,但是当我与condition3和condition4结合使用时,我得到一个空列表。
答案 0 :(得分:1)
@dbc,感谢您的帮助,但我可以通过以下方法解决此问题:
queryString = string.Format("$..steps[?(@.task == '{0}' && @.displayName =~
/^.*{1}.*$/)]", rule1, rule2);
List<Step> result1 = queryJson(queryString);
queryString = string.Format("$..steps[?(@.task == '{0}' && @.displayName =~
/^.*{1}.*$/)]", rule3, rule4);
List<Step> result2 = queryJson(queryString);
return result1.Union(result2).ToList();