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的更多相关文章

  1. [Node.js] DSL in action

    原文地址:http://www.moye.me/2015/05/30/dsl-in-action/ 最近看了本有意思的书,受到了一些启发,在此记录一下: DSLs in action   DSL是什么 ...

  2. 对json数据进行类似sql查询

    添加js引用:jsonsql-0.1.js 通过下面列子得到一个json类型的结果 Example: jsonsql.query("select * from json.channel.it ...

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

    查询json数据结构的8种方式 你有没有对“在复杂的JSON数据结构中查找匹配内容”而烦恼.这里有8种不同的方式可以做到: JsonSQL JsonSQL实现了使用SQL select语句在json数 ...

  4. JsonPath小结

    在查看DHC Assertions 模块说明的时候,无意间发现assert模块中JsonBody使用了 JSON Path ,兴趣使然,看了下,发现是类似解析xml用到的 XPath.通过路径来获取j ...

  5. [转载]查询json数据结构的8种方式

    http://wangxinghaoaccp.blog.163.com/blog/static/1158102362012111812255980/ 你有没有对“在复杂的JSON数据结构中查找匹配内容 ...

  6. 8种json数据查询方式

    你有没有对“在复杂的JSON数据结构中查找匹配内容”而烦恼.这里有8种不同的方式可以做到: JsonSQL JsonSQL实现了使用SQL select语句在json数据结构中查询的功能. 例子: ? ...

随机推荐

  1. MP3/WAV 播放

    一.编译libmad  1.先下载压缩包到本地,并解压  tar -xvzf  libmad-0.15.1b.tar.gz   -C   ./ 2.进入源代码文件夹并配置 编写一个配置文件,便于< ...

  2. idea maven jetty插件热部署

    maven tomcat插件好像无法进行热部署,jetty可以如下配置实现热部署,但是idea无法进行自动编译,所以需要如下快捷键 Ctrl+Shift+F9,编译 Ctrl+F9,生成项目 < ...

  3. PHP学习笔记十一【数组】

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/h ...

  4. 新浪微博iOS示例,登录,获取个人信息

    1.导入第三方库和系统框架

  5. html中的表格 bootstrap-table

    1 bootstrap-table 官网: http://bootstrap-table.wenzhixin.net.cn/ 2

  6. http://www.cnblogs.com/stephen-liu74/archive/2012/08/01/2561557.html

    http://www.cnblogs.com/stephen-liu74/archive/2012/08/01/2561557.html

  7. $digest already in progress

    最近在写项目时经常遇到一个小问题,在上下文外改变视图,通常来说我们需要apply()便可以实现,问题是加了apply()后控制台报错:$digest already in progress:随后我把a ...

  8. 解决ERROR C2011: 'FD_SET' : 'STRUCT' TYPE REDEFINITION问题

    在socket编程的过程中头文件中 #include <windows.h> #include "stdafx.h" #include "WinSock2.h ...

  9. ServletConfig和ServletContext

    ServletConfig和ServletContext Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为Servle ...

  10. C语言基础08

    1.指针和指针变量 指针变量:是保存变量地址的变量,存放只有地址; 指针:是变量的地址,存放的可以是3,4.5,YES; 普通变量与指针变量什么不同呢? 普通变量只能存取我们常常看到的类型数据,指针变 ...