当在复杂的JSON数组和散列中搜索项时,比如:
[
{ "id": 1, "name": "One", "objects": [
{ "id": 1, "name": "Response 1", "objects": [
// etc.
}]
}
]
是否有某种查询语言,我可以用来在[0]中找到一个项目。id = 3的对象?
当在复杂的JSON数组和散列中搜索项时,比如:
[
{ "id": 1, "name": "One", "objects": [
{ "id": 1, "name": "Response 1", "objects": [
// etc.
}]
}
]
是否有某种查询语言,我可以用来在[0]中找到一个项目。id = 3的对象?
当前回答
@Naftule -使用" defy .js",可以用XPath表达式查询JSON结构。看看这个计算器,了解它是如何工作的:
http://www.defiantjs.com/#xpath_evaluator
与JSONPath不同,“defy .js”提供了对查询语法的全面支持——对JSON结构上的XPath。
defy .js的源代码可以在这里找到: https://github.com/hbi99/defiant.js
其他回答
是的,它叫做JSONPath:
它还集成到DOJO中。
最新的XPath规范包括JSON支持:
XPath的主要目的是处理XML树和JSON树的节点。XPath得名于它使用路径符号在XML文档的层次结构中导航。XPath使用紧凑的非XML语法,以方便在uri和XML属性值中使用XPath。XPath 3.1为导航JSON树添加了类似的语法。
XQuery也是如此:
JSON是一种轻量级的数据交换格式,广泛用于在web上交换数据和在数据库中存储数据。许多应用程序将JSON与XML和HTML一起使用。XQuery 3.1扩展了XQuery以支持JSON和XML,向数据模型添加了映射和数组,并使用语言中的新表达式和[XQuery and XPath functions and Operators 3.1]中的新函数来支持它们。
我知道的其他选择是
JSONiq specification, which specifies two subtypes of languages: one that hides XML details and provides JS-like syntax, and one that enriches XQuery syntax with JSON constructors and such. Zorba implements JSONiq. Corona, which builds on top of MarkLogic provides a REST interface for storing, managing, and searching XML, JSON, Text and Binary content. MarkLogic 6 and later provide a similar REST interface as Corona out of the box. MarkLogic 8 and later support JSON natively in both their XQuery and Server-side JavaScript environment. You can apply XPath on it.
HTH.
索纳塔 https://jsonata.org/
它有一个操场,你可以下载图书馆
我知道OP用javascript标记了这个问题,但在我的情况下,我看起来完全一样,但从Java后端(与Camel)。
有趣的是,如果您使用的是像Camel这样的集成框架,jsonPath也由特定的Camel组件支持,从Camel 2.13开始。
例子来自上面的Camel文档:
from("queue:books.new")
.choice()
.when().jsonpath("$.store.book[?(@.price < 10)]")
.to("jms:queue:book.cheap")
.when().jsonpath("$.store.book[?(@.price < 30)]")
.to("jms:queue:book.average")
.otherwise()
.to("jms:queue:book.expensive")
使用起来很简单。