• 查询json数据结构的8种方式


    查询json数据结构的8种方式

    你有没有对“在复杂的JSON数据结构中查找匹配内容”而烦恼。这里有8种不同的方式可以做到:

    JsonSQL

    JsonSQL实现了使用SQL select语句在json数据结构中查询的功能。

    例子:

    1
    jsonsql.query("select * from json.channel.items order by title desc",json);

    主页:http://www.trentrichardson.com/jsonsql/

    JSONPath

    JSONPath就像是针对JSON数据结构的XPath。

    例子:

    1
    jsonPath( books, '$..book[(@.length-1)]')

    主页:http://goessner.net/articles/JsonPath/

    jfunk

    jFunk允许你检索(很快会加入管理功能)复杂的JSON或Javascript对象。jFunk API的设计几乎与jQuery API类似。它直接复制了jQuery的API,除了那些针对DOM的API。

    例子:

    1
    Jf("> vegetables > *[color=Orange]",Food).get();

    主页:http://code.google.com/p/jfunk/

    TaffyDB

    你过去有没有注意到Javascript对象的字面值看起来很像记录?如果你把他们包裹在一个数组里面,那么它们看起来有没有像一个数据库表?TaffyDB是一个Javascript库,它提供了强大的数据库功能以实现之前的想法,大大改善了你在Javascript中使用数据的方式。

    1
    var kelly = friends({id:2}).first();

    主页:http://www.taffydb.com/

    linq.js

    linq.js——Javascript中的LINQ(译者注:.Net中的概念,见http://msdn.microsoft.com/zh-tw/library/bb397897

    1
    2
    3
    4
    5
    var queryResult2 = Enumerable.From(jsonArray)
        .Where("$.user.id < 200")
        .OrderBy("$.user.screen_name")
        .Select("$.user.screen_name + ':' + $.text")
        .ToArray();

    主页:http://linqjs.codeplex.com/

    主页:http://neue.cc/reference.htm

    objeq

    objeq是一个简单的库,实现了对POJSO(Plain-Old JavaScript Objects,普通的Javascript对象)的实时查询。

    1
    2
    var res = $objeq(data, "age > 40 && gender == 'female' -> name");
        // --> Returns ['Jessica']

    主页:https://github.com/agilosoftware/objeq

    (译注:它使用了Javascript的property setters,所以它只能工作在较新的浏览器上)

    json:select()

    使用类CSS选择符来查询JSON。

    1
    .lang:val("Bulgarian") ~ .level

    主页:http://jsonselect.org/#tryit

    Paul的编程珠玑中的Javascript数组过滤方法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    var a = [1,2,3,4,5,6,7,8,9,10];
     
            // return everything
     
            a.where( "( ) => true" ) ;
     
            //  --> [1,2,3,4,5,6,7,8,9,10]
     
            // return even numbers
     
            a.where( "( n, i ) => n % 2 == 0" ) ;
     
            //  --> [2,4,6,8,10]
     
            // query first 6 products whose category begins with 'con' using extra param and regular expression
     
            products.where( "( el, i, res, param ) => res.length <= 6 && param.test( el.cat )", /^con/i);
     
            // using customer table data from SQL Server's northwind database...   
     
            customers.where( "( el, i, res, param ) => el.country == param""USA" );

    主页:http://www.paulfree.com/28/javascript-array-filtering/#more-28

    目前这是我最喜欢的查询JSON数据结构的方法。它非常的简单,并且据作者所说它非常快。

    它背后的理念和John Resig的JavaScript Micro-Templating类似:使用正确表达式将一段非常简单的字符串转换成Javascript函数。

    当然,还有更多强大的解决方案。Paul实现的原型还缺少对过滤表达式的语法检查,但是我相信你应该可以自己解决Javscript的语法检查。

    最后,你必须决定哪个对于你的项目来说最好。

    译文链接:http://blog.jobbole.com/31166/

  • 相关阅读:
    MySQL优化实例
    MySQL优化步骤和my.cnf优化配置
    linux高负载下mysql数据库彻底优化
    MySQL配置文件my.cnf详解
    CentOS Linux下MySQL 5.1.x的安装、优化和安全配置
    Apache 配置文件详解
    [LeetCode] Number of Boomerangs
    [LeetCode] Binary Watch
    [LeetCode] Reverse Linked List
    [LeetCode] Maximum Product of Three Numbers
  • 原文地址:https://www.cnblogs.com/timssd/p/5797241.html
Copyright © 2020-2023  润新知