3

对于大型 JSON 文件,JsonPath 似乎相当慢。

在我的项目中,我希望用户能够将整个查询作为字符串传递。我使用 JsonPath 是因为它可以让你一次完成整个$.store.book[3].price查询JsonPath.read(fileOrString, "$.store.book[3].price", new Filter[0])。是否有更快的方法与 Javascript 中的 JSON 文件进行交互?能够将整个查询作为字符串传递是理想的,但如果必须,我会编写一个解析器。有任何想法吗?

即使是小的优化也会有所帮助。例如,我目前每次查询时都从 JSON 文件中读取。将整个文件复制到开头的字符串并改为查询字符串会更好吗?

编辑:对于那些说“这是 Javascript,而不是 Java”的人,嗯,它实际上是 Java。JsonPath 是一种类似 Javascript 的查询语言,但我正在编写的文件肯定是 Java。只有查询是用 Javascript 编写的。以下是有关 JsonPath 的一些信息和一段代码:https ://code.google.com/p/json-path/

List toRet;
String query = "$.store.book[3].price";
try { 
    // if output is a list, good
    toRet = (List) JsonPath.read(filestring_, query, new Filter[0]);
} catch (ClassCastException cce) {
    // if output isn't a list, put it in a list
    Object outObj = null;
    try {
        outObj = JsonPath.read(filestring_, query, new Filter[0]);
    } catch (Exception e) {
        throw new DataSourceException("Invalid file!\n", e, DataSourceException.UNKNOWN);
    }
4

0 回答 0