[轉載]查詢json資料結構的8種方式

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   使用   java   io   

http://wangxinghaoaccp.blog.163.com/blog/static/1158102362012111812255980/

你有沒有對“在複雜的JSON資料結構中尋找匹配內容”而煩惱。這裡有8種不同的方式可以做到:

JsonSQL

JsonSQL實現了使用SQL select語句在json資料結構中查詢的功能。

例子:

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

首頁:http://www.trentrichardson.com/jsonsql/

JSONPath

JSONPath就像是針對JSON資料結構的XPath。

例子:

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

首頁:http://goessner.net/articles/JsonPath/

jfunk

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

例子:

Jf("> vegetables > *",Food).get();

首頁:http://code.google.com/p/jfunk/

TaffyDB

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

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

首頁:http://www.taffydb.com/

linq.js

linq.js——Javascript中的LINQ(譯者註:.Net中的概念,見http://msdn.microsoft.com/zh-tw/library/bb397897)

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

首頁:http://linqjs.codeplex.com/

首頁:http://neue.cc/reference.htm

objeq

objeq是一個簡單的庫,實現了對POJSO(Plain-Old JavaScript Objects,普通的Javascript對象)的即時查詢。

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

首頁:https://github.com/agilosoftware/objeq

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

json:select()

使用類CSS選擇符來查詢JSON。

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

首頁:http://jsonselect.org/#tryit

Paul的編程珠璣中的Javascript數組過濾方法

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" );

首頁:http://www.paulfree.com/28/javascript-array-filtering/#more-28

目前這是我最喜歡的查詢JSON資料結構的方法。它非常的簡單,並且據作者所說它非常快。

它背後的理念和John Resig的JavaScript Micro-Templating類似:使用正確運算式將一段非常簡單的字串轉換成Javascript函數。

當然,還有更多強大的解決方案。Paul實現的原型還缺少對過濾運算式的語法檢查,但是我相信你應該可以自己解決Javscript的語法檢查。

最後,你必須決定哪個對於你的項目來說最好。

 

英文原文:orangevolt,編譯:伯樂線上——周敏明

[轉載]查詢json資料結構的8種方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.