简体   繁体   English

如何在JSON上应用SQL where子句之类的复杂数据过滤器

[英]How to apply complex data filters like SQL where clause on JSON

I am Using Json to represent data at client side(javascript), I converted .net datatable object to json and now need to apply filter on Json data. 我正在使用Json在客户端(javascript)表示数据,我将.net datatable对象转换为json,现在需要对Json数据应用过滤器。 I used _.where to apply basic data filter , but need complex filtering like: 我使用_.where来应用基本数据过滤器,但需要像这样的复杂过滤:

where = (stundentID = 55 and school='abc') AND (City='xyz' or City ='abc')

Its a basic type of filtration that we used in Oracle or sql server queries. 它是我们在Oracle或sql服务器查询中使用的基本过滤类型。 Now how can i apply such mechanism using underscore.js or some thing equivelant on Json or javascript structures. 现在,我如何使用underscore.js或Json或javascript结构上的某些东西应用这种机制。 I might also need to do query of getting Top N record , using aggregation [ Sum(sales)==500] at javascript. 我可能还需要使用JavaScript的聚合[Sum(sales)== 500]进行查询以获取前N个记录。 Waiting for your valuable suggestions. 等待您的宝贵建议。

Thanks, 谢谢,

Assuming you have this JSON represented as a regular JavaScript object—there's no such thing as a JSON object, after all—you're best bet would likely be to use linq.js 假设您将此JSON表示为常规JavaScript对象-毕竟没有JSON对象之类的东西-最好是使用linq.js

You'd be able to do things like: 您将可以执行以下操作:

var studentFilteredArray = 
          Enumerable.From(students)
                    .Where(function(s) { return s.studenId == 55;  })
                    .OrderBy(function(s) { return s.LastName; })
                    .ToArray();

Or from your example above: 或从上面的示例中:

.Where(function(s) { 
      return s.studentID === 55 && s.school === 'abc' && (s.City === 'xyz' || s.City === 'abc') 
});

You also can try alasql.js , where you can use standard SQL SELECT WHERE statement. 您也可以尝试使用alasql.js ,在其中可以使用标准的SQL SELECT WHERE语句。 Alasql was specially designed to work with JSON arrays like tables in database. Alasql专门设计用于处理JSON数组,例如数据库中的表。

// Create database and describe the table
var db = new alasql.Database();
db.exec('CREATE TABLE students ([studentID] INT, school STRING, [City] STRING)');

// Assign you array here
db.tables.students.data = [
       {studentID: 55, school: 'abc', City:'abc'},
       {studentID: 56, school: 'klm', City:'xyz'},
       {studentID: 57, school: 'nyz', City:'xyz'}
    ];

// Use regular SQL 
console.log(db.exec("SELECT * FROM students "
  +" WHERE ([studentID] = 55 AND school='abc') AND ([City]='xyz' OR [City] ='abc')"));

Try this example in jsFiddle . 在jsFiddle中尝试此示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过 kendo 数据源以编程方式应用复杂的 odata 过滤器? - How to apply complex odata filters programatically with kendo data sources? 需要对 JSON 数据应用多个过滤器 - Need to apply multiple filters to JSON data 如何在JavaScript中将过滤器应用于JSON数组? - How to apply filters to an array of JSON in JavaScript? 如何使用把手来处理像这样的复杂JSON数据? - How to use handlebars to handle complex JSON data like this? jQuery通过“ where子句”遍历复杂的JSON结构 - jquery each iterating over complex JSON structure with “where clause” 如何使用多个`==`s,多个`&gt; =`,多个`&lt;=`范围过滤器链接where子句 - How to chain where clause with multiple `==`s , multiple `>=`, multiple `<=` range filters 在jcanvas中如何操纵drawImage层以将instagram像滤镜一样应用到图像上 - In jcanvas how to manipulate drawImage layer to apply instagram like filters on the image 如何一起应用过滤器? - How to apply filters together? 如何计算多对多关系并使用 ORM Sequelize 应用 where 子句 - How to count a many to many relationships and apply where clause with ORM Sequelize 将过滤器应用于列表并显示数据 - Apply filters to a list and show data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM