当在复杂的JSON数组和散列中搜索项时,比如:

[
    { "id": 1, "name": "One", "objects": [
        { "id": 1, "name": "Response 1", "objects": [
            // etc.
        }]
    }
]

是否有某种查询语言,我可以用来在[0]中找到一个项目。id = 3的对象?


当前回答

我知道的其他选择是

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.

其他回答

js看起来也很酷,这里有一个简单的例子:

var obj = {
        "car": [
            {"id": 10, "color": "silver", "name": "Volvo"},
            {"id": 11, "color": "red",    "name": "Saab"},
            {"id": 12, "color": "red",    "name": "Peugeot"},
            {"id": 13, "color": "yellow", "name": "Porsche"}
        ],
        "bike": [
            {"id": 20, "color": "black", "name": "Cannondale"},
            {"id": 21, "color": "red",   "name": "Shimano"}
        ]
    },
    search = JSON.search(obj, '//car[color="yellow"]/name');

console.log( search );
// ["Porsche"]

var reds = JSON.search(obj, '//*[color="red"]');

for (var i=0; i<reds.length; i++) {
    console.log( reds[i].name );
}
// Saab
// Peugeot
// Shimano

Json指针似乎也得到了越来越多的支持。

我认为JSONQuery是JSONPath的超集,因此在dojo中取代了JSONPath。还有RQL。

来自Dojo文档:

JSONQuery是JSONPath的扩展版本,具有其他特性 为安全性、易用性和全面的数据查询集 工具包括过滤,递归搜索,排序,映射,范围 选择,以及具有通配符字符串比较的灵活表达式 以及各种运算符。

JSONselect对这个问题有另一种观点(类似CSS选择器,而不是XPath),并有一个JavaScript实现。

试试这个——https://github.com/satyapaul/jpath/blob/master/JSONDataReader.java

这是一个非常简单的实现,类似于xpath for xml。它的名称是jpath。

@Naftule -使用" defy .js",可以用XPath表达式查询JSON结构。看看这个计算器,了解它是如何工作的:

http://www.defiantjs.com/#xpath_evaluator

与JSONPath不同,“defy .js”提供了对查询语法的全面支持——对JSON结构上的XPath。

defy .js的源代码可以在这里找到: https://github.com/hbi99/defiant.js