PHP JSONPath, XPath contains filter
I using JSONPath with PHP and i need the xpath equivalent to the contains filter, "contains(dept, 'Management')";
I have the following JSON and i want to filter the staff members to the Management Dept only. If the department node only contains 1 department then it's no problem, but i need find the keyword within department string?
Any help would be appreciated.
{
"staff": {
"members": [
{
"firstname": "J",
"lastname": "H",
"department": "Management, Finance"
},
开发者_高级运维 {
"firstname": "S",
"lastname": "M",
"department": "Finance"
}
]
}
}
$parser->setPath("$..members[?(@['department']=='Management')]");
$staff = $parser->getData();
var_dump($staff);
Look at this
JSONPath :contains filter
I would assume in your case wil be something like.
"$..members[?(/Management/.test(@['department']))";
I managed to figure this out. There are examples found in http://jsonpath.googlecode.com/svn/trunk/tests/jsonpath-test-php.php which use PHP strpos, but this didn't work for me initially using jsonpath-0.8.1.php.
I gave jsonpath-0.8.3.php found the the repository and the strpos expression worked perfectly:-
$..members[?(@ && @['department'] && strpos(@['department'],'".$dept."')!==false)]
Note, the function evalx() has a third parameter which causes errors, setting the value to null works fine, see http://code.google.com/p/jsonpath/issues/detail?id=8
function evalx($x, $v, $vname=null)
精彩评论