jsonsql
http://www.trentrichardson.com/jsonsql/
可以对json数组用sql语法进行操作,主要是查询取指定字段、条件、指定字段排序及获取多少条数据,返回值json。
jsonsql.query("select * from json.channel.items order by title desc",json);
jsonsql.query("select title,url from json.channel.items where (category=='javascript' || category=='vista') order by title,category asc limit 3",json);
开发例子:
var hospitals=[{"a":"1","b":"2","c":"3"},{"a":"3","b":"4","c":"5"}];
var b=jsonsql.query("select a,b from json where (a=='3') order by a ",hospitals);
结果:
[{
"a" : "3",
"b" : "4"
}]
js库源码
/*
* JsonSQL
* By: Trent Richardson [http://trentrichardson.com]
* Version 0.1
* Last Modified: 1/1/2008
*
* Copyright 2008 Trent Richardson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var jsonsql = {
query: function(sql,json){
var returnfields = sql.match(/^(select)\s+([a-z0-9_\,\.\s\*]+)\s+from\s+([a-z0-9_\.]+)(?: where\s+\((.+)\))?\s*(?:order\sby\s+([a-z0-9_\,]+))?\s*(asc|desc|ascnum|descnum)?\s*(?:limit\s+([0-9_\,]+))?/i);
var ops = {
fields: returnfields[2].replace(' ','').split(','),
from: returnfields[3].replace(' ',''),
where: (returnfields[4] == undefined)? "true":returnfields[4],
orderby: (returnfields[5] == undefined)? []:returnfields[5].replace(' ','').split(','),
order: (returnfields[6] == undefined)? "asc":returnfields[6],
limit: (returnfields[7] == undefined)? []:returnfields[7].replace(' ','').split(',')
};
return this.parse(json, ops);
},
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;
},
returnFilter: function(json,jsonsql_o){
var jsonsql_scope = eval(jsonsql_o.from);
var jsonsql_result = [];
var jsonsql_rc = 0;
if(jsonsql_o.where == "")
jsonsql_o.where = "true";
for(var jsonsql_i in jsonsql_scope){
with(jsonsql_scope[jsonsql_i]){
if(eval(jsonsql_o.where)){
jsonsql_result[jsonsql_rc++] = this.returnFields(jsonsql_scope[jsonsql_i],jsonsql_o.fields);
}
}
}
return jsonsql_result;
},
returnFields: function(scope,fields){
if(fields.length == 0)
fields = ["*"];
if(fields[0] == "*")
return scope;
var returnobj = {};
for(var i in fields)
returnobj[fields[i]] = scope[fields[i]];
return returnobj;
},
returnOrderBy: function(result,orderby,order){
if(orderby.length == 0)
return result;
result.sort(function(a,b){
switch(order.toLowerCase()){
case "desc": return (eval('a.'+ orderby[0] +' < b.'+ orderby[0]))? 1:-1;
case "asc": return (eval('a.'+ orderby[0] +' > b.'+ orderby[0]))? 1:-1;
case "descnum": return (eval('a.'+ orderby[0] +' - b.'+ orderby[0]));
case "ascnum": return (eval('b.'+ orderby[0] +' - a.'+ orderby[0]));
}
});
return result;
},
returnLimit: function(result,limit){
switch(limit.length){
case 0: return result;
case 1: return result.splice(0,limit[0]);
case 2: return result.splice(limit[0]-1,limit[1]);
}
}
};
jsonsql的更多相关文章
- [Node.js] DSL in action
原文地址:http://www.moye.me/2015/05/30/dsl-in-action/ 最近看了本有意思的书,受到了一些启发,在此记录一下: DSLs in action DSL是什么 ...
- 对json数据进行类似sql查询
添加js引用:jsonsql-0.1.js 通过下面列子得到一个json类型的结果 Example: jsonsql.query("select * from json.channel.it ...
- 查询json数据结构的8种方式
查询json数据结构的8种方式 你有没有对“在复杂的JSON数据结构中查找匹配内容”而烦恼.这里有8种不同的方式可以做到: JsonSQL JsonSQL实现了使用SQL select语句在json数 ...
- JsonPath小结
在查看DHC Assertions 模块说明的时候,无意间发现assert模块中JsonBody使用了 JSON Path ,兴趣使然,看了下,发现是类似解析xml用到的 XPath.通过路径来获取j ...
- [转载]查询json数据结构的8种方式
http://wangxinghaoaccp.blog.163.com/blog/static/1158102362012111812255980/ 你有没有对“在复杂的JSON数据结构中查找匹配内容 ...
- 8种json数据查询方式
你有没有对“在复杂的JSON数据结构中查找匹配内容”而烦恼.这里有8种不同的方式可以做到: JsonSQL JsonSQL实现了使用SQL select语句在json数据结构中查询的功能. 例子: ? ...
随机推荐
- hdu2095 像水题的不错题 异或运算
异或运算的基础有点忘记了 先介绍一下..2个数异或 就是对于每一个二进制位进行位运算 具有2个特殊的性质 1.一个数异或本身恒等于0,如5^5恒等于0: 2.一个数异或0恒等于本身,如5^0恒等于5. ...
- C#中decimal的用法
decimal拥有比float更高的精度,最高能处理到小数点后面的28位.适合用在财务类等对数字精确度要求比较高的场合. using System; using System.Collections. ...
- Winform单例模式与传值
单例模式(singleton)的意思就是只有一个实例.单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.这个类称为单例类. 在多窗体界面中,如果要加入一个“关于”的窗体,用于显 ...
- js获取昨天日期
刚刚js做项目,遇到需要获取昨天日期的问题,网上找了下答案,感觉网上的答案都不太严谨,自己写了个,凑合能用吧,忘大神们抛砖指教. <script type="text/javascri ...
- js自定义事件、DOM/伪DOM自定义事件
一.说明.引言 我JS还是比较薄弱的,本文的内容属于边学边想边折腾的碎碎念,可能没什么条理,可能有表述不准确的地方,可能内容比较拗口生僻.如果您时间紧迫,或者JS造诣已深,至此您就可以点击右侧广告(木 ...
- TCP的拥塞控制(转载)
1.引言 计算机网络中的带宽.交换结点中的缓存和处理机等,都是网络的资源.在某段时间,若对网络中某一资源的需求超过了该资源所能提供的可用部分,网络的性能就会变坏.这种情况就叫做拥塞. 拥塞控制就是防止 ...
- 我用爬虫一天时间“偷了”知乎一百万用户,只为证明PHP是世界上最好的语言
我用爬虫一天时间“偷了”知乎一百万用户,只为证明PHP是世界上最好的语言 2015-08-06 猿圈 我用爬虫一天时间“偷了”知乎一百万用户 只为证明PHP是世界上最好的语言 看了不少朋友圈里推荐的P ...
- Android 贝塞尔曲线
博客图片备份位置:
- iframe自适应高度的多种方法方法小结(转)
对于自适应高度的代码有很多,可效率什么的考虑进来好代码就不多见了,不过思路倒是差不多的不带边框的iframe因为能和网页无缝的结合从而不刷新页面的情况下更新页面的部分数据成为可能,可是 iframe的 ...
- python交换两个变量的值,一句代码搞定
a = 10 b = 20 # 不需要中间变量,一步搞定 a, b = b, a