我有一个JSON,其中包含一个值,该值是由管道分隔的键值对组成的字符串,我需要使用脚本使用JSONPath表达式提取值。
我希望返回所有的"职位名称"。位于"行"的第三个位置。
这是原始JSON
{
"DataSource": {
"Rows": [
[
"Leslie Knope",
"Eastern Standard Time",
"USA",
"Department:Parks and Recreation|Job Title:Project Manager|Email:leslie@pawnee.il.gov"
],
[
"Ron Swanson",
"Eastern Standard Time",
"USA",
"Department:Parks and Recreation|Job Title:Senior Project Manager|Email:ron@pawnee.il.gov"
]
]
}
}
要选择我需要的细节,我将使用JSONPath表达式"$.DataSource.Rows[*].3"它会返回:
[
"Department:Parks and Recreation|Job Title:Project Manager|Email:leslie@pawnee.il.gov",
"Department:Parks and Recreation|Job Title:Senior Project Manager|Email:ron@pawnee.il.gov"
]
我知道我可以使用正则表达式来提取"Job Title"正则表达式是"/(& lt; =头衔 :)(.*?)(?=[|]|$)/gm" .
是否有一种方法可以使用JSON路径(连同Regex)从JSON中仅返回"职位名称"的值?
我要找的输出是:
[
"Project Manager",
"Senior Project Manager"
]
这是可能做到这一点使用JSONPath与脚本?我难住了!
可以使用map函数代替
var result = json.DataSource.Rows.map((element) => {
return element[3].substring(
element[3].indexOf("Title:") + 6,
element[3].indexOf("Email:") - 1
);
});
或者如果你想要json path
var result = jsonPath(json, "$.DataSource.Rows[*].3").map((element) => {
return element.substring(
element.indexOf("Title:") + 6,
element.indexOf("Email") - 1
);
});