查詢json的資料結構的8種方式簡介

來源:互聯網
上載者:User

查詢json的資料結構的8種方式:

JsonSQL

JsonSQL實現了使用SQL select語句在json資料結構中查詢的功能。首頁:http://www.trentrichardson.com/jsonsql/

例子:
複製代碼 代碼如下:
jsonsql.query("select * from json.channel.items order by title desc",json);

JSONPath

JSONPath就像是針對JSON資料結構的XPath。首頁:http://goessner.net/articles/JsonPath/

例子:
複製代碼 代碼如下:
jsonPath( books, '$..book[(@.length-1)]')

jfunk

jFunk允許你檢索(很快會加入管理功能)複雜的JSON或Javascript對象。jFunk API的設計幾乎與jQuery API類似。它直接複製了jQuery的API,除了那些針對DOM的API。
首頁:http://code.google.com/p/jfunk/

例子:
複製代碼 代碼如下:
Jf("> vegetables > *[color=Orange]",Food).get();

TaffyDB

你過去有沒有注意到Javascript對象的字面值看起來很像記錄?如果你把他們包裹在一個數組裡面,那麼它們看起來有沒有像一個資料庫表?TaffyDB是一個Javascript庫,它提供了強大的資料庫功能以實現之前的想法,大大改善了你在Javascript中使用資料的方式。
首頁:http://www.taffydb.com/

例子:
複製代碼 代碼如下:
var kelly = friends({id:2}).first();

linq.js

linq.js——Javascript中的LINQ
複製代碼 代碼如下:
var queryResult2 = Enumerable.From(jsonArray)
    .Where("$.user.id < 200")
    .OrderBy("$.user.screen_name")
    .Select("$.user.screen_name + ':' + $.text")
    .ToArray();

objeq

objeq是一個簡單的庫,實現了對POJSO(Plain-Old JavaScript Objects,普通的Javascript對象)的即時查詢。首頁:https://github.com/agilosoftware/objeq
複製代碼 代碼如下:
var res = $objeq(data, "age > 40 && gender == 'female' -> name");
// --> Returns ['Jessica']

(譯註:它使用了Javascript的property setters,所以它只能工作在較新的瀏覽器上)

json:select()

使用類CSS選擇符來查詢JSON。首頁:http://jsonselect.org/#tryit
複製代碼 代碼如下:
.lang:val("Bulgarian") ~ .level

Paul的編程珠璣中的Javascript數組過濾方法,首頁:http://www.paulfree.com/28/javascript-array-filtering/#more-28
複製代碼 代碼如下:
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" );

目前這是我最喜歡的查詢JSON資料結構的方法。它非常的簡單,並且據作者所說它非常快。
它背後的理念和John Resig的JavaScript Micro-Templating類似:使用正確運算式將一段非常簡單的字串轉換成Javascript函數。
當然,還有更多強大的解決方案。Paul實現的原型還缺少對過濾運算式的語法檢查,但是我相信你應該可以自己解決Javscript的語法檢查。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.