Home >Web Front-end >JS Tutorial >Introduction to 8 ways to query the data structure of json_Basic knowledge

Introduction to 8 ways to query the data structure of json_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 16:55:54886browse

8 ways to query the data structure of json:

JsonSQL

JsonSQL implements the function of querying in the json data structure using SQL select statements. Home page: http://www.trentrichardson.com/jsonsql/

Example:

Copy code The code is as follows:

jsonsql.query("select * from json.channel.items order by title desc",json);

JSONPath

JSONPath is like XPath for JSON data structures. Home page: http://goessner.net/articles/JsonPath/

Example:

Copy code The code is as follows:

jsonPath( books, '$. .book[(@.length-1)]')

jfunk

jFunk allows you to retrieve (and soon manage) complex JSON or Javascript objects. The design of jFunk API is almost similar to jQuery API. It directly copies jQuery's API, except for those targeting the DOM.
Homepage: http://code.google.com/p/jfunk/

Example:

Copy code The code is as follows:

Jf("> vegetables > ; *[color=Orange]",Food).get();

TaffyDB

Have you ever noticed in the past that Javascript object literals look a lot like records? If you wrap them in an array, do they look like a database table? TaffyDB is a Javascript library that provides powerful database functionality to implement previous ideas, greatly improving the way you work with data in Javascript.
Homepage: http://www.taffydb.com/

Example:

Copy code The code is as follows:

var kelly = friends({id :2}).first();

linq.js

linq.js - LINQ in Javascript

Copy code The code is as follows:

var queryResult2 = Enumerable.From(jsonArray)
.Where("$.user.id < 200")
.OrderBy("$.user.screen_name")
.Select("$.user .screen_name ':' $.text")
.ToArray();

objeq

objeq is a simple library that implements real-time query of POJSO (Plain-Old JavaScript Objects, ordinary Javascript objects). Home page: https://github.com/agilosoftware/objeq

Copy code The code is as follows:

var res = $objeq(data, "age > 40 && gender == 'female' -> name");
// --> Returns ['Jessica']

(Annotation: It uses Javascript property setters, so it can only work on newer browsers)

json:select()

Query JSON using CSS-like selectors. Home page: http://jsonselect.org/#tryit

Copy code The code is as follows:

.lang:val("Bulgarian") ~ .level

Javascript array filtering method in Paul’s Programming Pearls, homepage: http://www.paulfree.com/28/javascript-array-filtering/#more-28

Copy code The code is as follows:

var a = [1,2,3,4,5,6,7, 8,9,10];
// return everything
a.where( "( ) => true" ) ;
// --> [1,2,3,4,5 ,6,7,8,9,10]
// return even numbers
a.where( "( n, i ) => n % 2 == 0" ) ;
// - -> [2,4,6,8,10]
// query first 6 products whose category begins with 'con' using extra param and regular expression
products.where( "( el, i, res , param ) => res.length <= 6 && param.test( el.cat )", /^con/i);
// using customer table data from SQL Server's northwind database... "
customers.where( "( el, i, res, param ) => el.country == param", "USA" );

Currently this is my favorite way to query JSON data structures. It's very simple, and according to the author it's very fast.
The idea behind it is similar to John Resig's JavaScript Micro-Templating: use the correct expression to convert a very simple string into a Javascript function.
Of course, there are more powerful solutions. The prototype implemented by Paul also lacks syntax checking for filter expressions, but I believe you should be able to solve Javscript's syntax checking yourself.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn