Web前端工作笔记002---json数据查询的方法_json查询大全,JsonSQL数据查询,jfunk数据查询

  JAVA技术交流QQ群:170933152  

json数据查询的方法

网上看到有一篇帖子,有8种json数据查询的方法,大家可以研究一下,我现在分享一下!

JsonSQL

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

例子:

$.getJSON("testjson.js", function(a){
    console.dir(jsonsql.query("select * from json.channel.items order by title desc,json",a));
});

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

JSONPath

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

例子:

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

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

jfunk

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

例子:

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

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

TaffyDB

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

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)

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

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

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

objeq

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

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

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

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

json:select()

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

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

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

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

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查询 项目背景和应用

项目背景

做一个微信项目,需要对返回的json数据进行搜索和查询,查询是模糊查询,json数据结构比较复杂。

应用

我一开始用的是JsonSQL,可以进行简单的json数据查询,比较方便,但是用起来发现JsonSQL查询的json数据结构比较简单,对于复杂的,嵌套很多的数据,用起来不是很方便。因此,对于JsonSQL,我最后没有用它。

JsonSQL的应用:

function getAll(){
                $.getJSON("testjson.js", function(a){
                    console.dir(jsonsql.query("select * from json.channel.items order by title desc,json",a));
                });
    }

 function getFiltered(){
                $.getJSON("testjson.js", function(json){
                    console.dir(jsonsql.query("select title,url,author,category from json.channel.items where (category=='javascript' || author=='trent') order by title,category asc limit 3",json));
                });
}

function getLimit(){
                $.getJSON("testjson.js", function(json){
                    console.dir(jsonsql.query("select url from json.channel.items where (category=='javascript' && author=='trent') order by url asc limit 1,2",json));
                });
    }

引进的是如下2个文件。

<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script type="text/javascript" src="jsonsql-0.1.js"></script>

testjson.js的结构是

{
    "channel": {
        "title": "Trent's Blog",
        "link": "http://www.haorooms.com/",
        "description": "practical programming",
        "published": "Fri, 28 Dec 20014 04:27:29 +0000",
        "language": "en",
        "items": [{
            "title": "Are You Using Regular Expressions Within SQL?",
            "url": "http://www.haorooms.com/?p=45",
            "published": "Fri, 28 Dec 2014 02:32:31 +0000",
            "author": "trent",
            "category": "programming",
            "guid": {
                "isPermaLink": false,
                "url": "http://www.haorooms.com/?p=45"
            },
            "description": "Are You Using Regular Expressions Within SQL?Are You Using Regular Expressions Within SQL?Are You Using Regular Expressions Within SQL?"
        },{
            "title": "Nothing Better than a Good Dump!",
            "url": "http://www.haorooms.com/?p=44",
            "published": "Mon, 24 Dec 2013 02:54:39 +0000",
            "author": "trent",
            "category": "javascript",
            "guid": {
                "isPermaLink": false,
                "url": "http://www.haorooms.com/?p=44"
            },
            "description": "Nothing Better than a Good Dump!Nothing Better than a Good Dump!Nothing Better than a Good Dump!Nothing Better than a Good Dump!Nothing Better than a Good Dump!"
        }]
    }
}

JsonSQL对这种简单的数据结构,查询起来比较方便。注意:jsonSQL的代码得知:

parse: function(json,ops){
        var o = { fields:["*"], from:"json", where:"", orderby:[], order: "asc", limit:[] };
        for(i in ops) o[i] = ops[i];

        var result = [];        
        result = this.returnFilter(json,o);
        result = this.returnOrderBy(result,o.orderby,o.order);
        result = this.returnLimit(result,o.limit);

        return result;
    },

JsonSQl只支持简单的SQL查询,例如where,orderBy,order,且where只能跟“==”不能进行模糊查询!不能用like,否则会出错!!!

另外,JsonSQl的键值key只能是英文,不能有中文,否则会出错!!!

jfunk的应用:

可以对比较复杂的数据结构进行操作

var Food={
    fruits: [
        { name: "Banana", color: "Yellow" },
        { name: "Apple", color: "Red" },
        { name: "Grapefruit", color: "Orange" },
        { name: "Kiwi", color: "Green" }
        ],
    vegetables: [
        { name: "Carrot", color: "Orange" },
        { name: "Turnip", color: "Purple" },
        { name: "Rutabaga", color: "Yellow" },
        { name: "Sweet Potato", color: "Orange" }
        ]
    };

var orangeStuff=jF("*[color=Orange]",Food).get();
var orangeVeg  =jF("> vegetables > *[color=Orange]",Food).get();

//orange stuff is now [{name:"Grapefruit",color:"Orange"},{name:"Carrot",color:"Orange"},{name:"Sweet Potato",color:"Orange"}]
//orange veg is now [{name:"Carrot",color:"Orange"},{name:"Sweet Potato",color:"Orange"}]

项目应用

因为我的数据很复杂,最后我选用的是jfunk,但是我没有完全运用jfunk,我只是用jF("> vegetables > *[color]",Food).get();这种方法,查询到了vegetables 子类中的所有color,并对color的某些属性,循环判断,模糊查询属性中有没有某个值,然后把查询到的结果push到了新的数组当中。这样完成的搜索结果。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值