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数据结构中查询的功能. 例子: ? ...
随机推荐
- 王立平-Android中对图像进行Base64编码
// ------------------base64-------------------// public String bitmaptoString(Bitmap bitmap) { // 将B ...
- 改变页面选择文字颜色和背景颜色----selection伪元素
div::selection{color:#fff;background: #E83E84;text-shadow:none}
- 如何查看SQLServer数据库每个表占用的空间大小?
如何查看SQLServer数据库每个表占用的空间大小? 创建存储过程: CREATE PROCEDURE [dbo].[sys_viewTableSpace]AS BEGIN SET NOCOUNT ...
- HDU 2653 - Waiting ten thousand years for Love
首先,对于一个 '@' 飞上去,飞下来都要耗1点魔力,所以是两点= = 然后站在同一格 魔力可能不同,所以要增加一维. 还有当前搜到的不一定是最小. 别的也没啥. #include <iostr ...
- mybatis常用jdbcType数据类型
MyBatis 通过包含的jdbcType类型 BIT FLOAT CHAR TIMESTAMP OTHER UNDEFINED ...
- ORACLE DUAL表详解 .
今天在戴明明同学的QQ空间里看到篇不错的关于DUAL表的文章,自己平时也时而会用到,可是没有系统的学习过,借这个机会学习学习~ ==================================== ...
- 容联手机接口封装到ThinkPHP3.2.菜鸟图文教学
今天来说下短信发送技术. 使用的是 容联http://www.yuntongxun.com/ 用法很简单, 具体要知道的参数有 ACCOUNT SID 应用ID AUTH TOKEN 应用toke ...
- BaseActivity的定义——作为所有Activity类的父类
public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener { prote ...
- SSO单点登录(转载)
昨天和几位朋友探讨到了这个话题,发现虽然单点登录,或者叫做独立的passport登录虽然已经有了很多实现方法,但是能真正了解并实现的人却并不太多,所以些下此文,希望从原理到实现,能让大家了解的多一些 ...
- apache配置gzip模式(转)
最近在看有关提高website性能的书,个人觉得挺不错的两本书: <<high performance websites>> <<even faster websi ...