根据JSONPATH中数组中的字符串过滤Json [英] filter the Json according to string in an array in JSONPATH
问题描述
我遇到的情况是,我的json字符串的子级为Array,仅包含字符串.有什么办法可以获取包含特定String的数组的对象引用. 示例:
I have a situation where I have json String that has a child as Array that contains only Strings. Is there as way I can get the object reference of the arrays that contains a specific String. Example:
{ "Books":{
"History":[
{
"badge":"y",
"Tags":[
"Indian","Culture"
],
"ISBN":"xxxxxxx",
"id":1,
"name":"Cultures in India"
},
{
"badge":"y",
"Tags":[
"Pre-historic","Creatures"
],
"ISBN":"xxxxxxx",
"id":1,
"name":"Pre-historic Ages"
}
]
}
}
要实现: 从上面的JSON字符串中,需要获取历史记录中标签"列表中包含印度语"的所有书籍.
To Achieve: From the above JSON String, need to get all books in History which contains "Indian" inside the "tags" list.
我在项目中使用的是JSONPATH,但如果有其他API可以提供类似的功能,欢迎任何帮助.
I am using JSONPATH in my project but If there is other API that can provide similar functionality, any help is welcome.
推荐答案
如果您使用的是Goessner JSONPath,则请参阅Duncan
If you're using Goessner JSONPath, $.Books.History[?(@.Tags.indexOf('Indian') != -1)]
as mentioned by Duncan above should work.
如果您正在使用Jayway Java端口(github.com/jayway/JsonPath),则
$.Books.History[?(@.Tags[?(@ == 'Indian')] != [])]
或更优雅地使用in
运算符,例如$.Books.History[?('Indian' in @.Tags)]
.在此处进行了尝试.
If you're using the Jayway Java port (github.com/jayway/JsonPath), then
$.Books.History[?(@.Tags[?(@ == 'Indian')] != [])]
or more elegantly, use the in
operator like this $.Books.History[?('Indian' in @.Tags)]
. Tried them both here.
这篇关于根据JSONPATH中数组中的字符串过滤Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!