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. kaggle之旧金山犯罪

    kaggle地址 github地址 特点: 离散特征 离散特征二值化处理 数据概览 import pandas as pd import numpy as np # 载入数据 train = pd.r ...

  2. 【贪心】【uva11520】 Fill the Square

    填充正方形(Fill the Square, UVa 11520) 在一个n×n网格中填了一些大写字母,你的任务是把剩下的格子中也填满大写字母,使得任意两个相邻格子(即有公共边的格子)中的字母不同.如 ...

  3. openssl 安装

    六.运行“nmake -f ms\ntdll.mak install”安装编译后的OpenSSL到指定目录. 七.查看安装结果C:\usr\local\ssl或C:\openssl-0.9.8.e下包 ...

  4. 《JavaScript 闯关记》之数组

    数组是值的有序集合.每个值叫做一个元素,而每个元素在数组中有一个位置,以数字表示,称为索引. JavaScript 数组是无类型的,数组元素可以是任意类型,并且同一个数组中的不同元素也可能有不同的类型 ...

  5. session之退出登陆

    <span style="font-size:32px;">//使用SESSION必须先开启session session_start(); //彻底删除session ...

  6. html5 canvas 一个漫天飞雪的效果

    很棒的下雪效果 代码奉上 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: ...

  7. wordpress一些常用代码

    显示最新文章 <div id="newpost"> <h2> 最新文章</h2> <?php $previous_posts = get_ ...

  8. openstack安装记录(二)keystone安装

    先决条件 在你配置 OpenStack 身份认证服务前,你必须创建一个数据库和管理员令牌. 完成下面的步骤以创建数据库: 用数据库连接客户端以 root 用户连接到数据库服务器: $ mysql -u ...

  9. chart

    var pieChart = { width: 600, height: 400, series: [], unit: "kg", chartCanvas: null, selec ...

  10. JVM调优的几种策略(转)

    JVM参数调优是一个很头痛的问题,可能和应用有关系,别人说可以的对自己不一定管用.下面是本人一些JVM调优的实践经验,希望对读者能有帮助,环境LinuxAS4,resin2.1.17,JDK6.0,2 ...