在jsonPath中我试图过滤具有多个值的对象。一个可能的值是电子邮件地址。由于@被用作指向当前对象的指针,因此它失败了。
示例json string
{
"report": {
"Author": [
{
"value": "John",
"count": "58",
"fields": [
{
"key": "FileSize",
"value": "12314"
},
{
"key": "Date",
"value": "2000-01-01"
}
]
},
{
"value": "Jane",
"count": "1",
"fields": [
]
},
{
"value": "Foo@bar.com",
"count": "58",
"fields": [
{
"key": "FileSize",
"value": "12314"
},
{
"key": "Date",
"value": "2000-01-01"
},
{
"key": "tags",
"value": "a,b,c,d"
}
]
},
{
"value": "Bill",
"count": "2",
"fields": [
{
"key": "FileSize",
"value": "14"
}
]
}
]
}
}
如果您使用
$.report.Author[?(@.value==='John')]
结果是
"[{"value":"John","count":"58","fields":[{"key":"FileSize","value":"12314"},{"key":"Date","value":"2000-01-01"}]}]"
但由于电子邮件地址有@符号,jsonPath在
上返回false$.report.Author[?(@.value==='Foo@bar.com')]
任何人都知道如何通过@符号或不同的方法来逃避对象?
答案 0 :(得分:1)
答案 1 :(得分:0)
您可以使用DefiantJS并在JSON结构上编写XPath。试试lib并在你的JSON上测试XPath:
http://defiantjs.com/#xpath-evaluator
我已经粘贴了你的json数据并测试了这个XPath,它可以工作:
//Author[value="Foo@bar.com"]
DefiantJS使用新方法扩展全局对象JSON; “搜索” - 返回一个数组,在javascript中,它看起来像这样:
var author = JSON.search(json_data, "//Author[value="Foo@bar.com"]");
console.log( author[0].value );
// Foo@bar.com
console.log( author[0].count );
// 58