在我们的研发过程中,有时会遇到这样的情况:程序很简单,数据是固定的,不想使用服务端语言(php、java、.net),但是要实现一些分页和检索功能。这种情况下我们应该选择javascript+xml+div就ok,不过javascript解析xml的数据比较慢。json是现在非常流行的一种数据格式,所以有另外一种解决方案:javascript+json+div。

json是目前web的通用数据格式,在轻量级的web服务中占据主导地位,这里推荐一个非常轻巧的类库:jsonpath(下载地址:http://code.google.com/p/jsonpath/)。作为一个轻量级类库,你可以使用它来对json数据进行精确查找、条件查找和模糊查找,这样就能满足简单网站的功能了。下面是一个简单例子: JSONPath - Example (js)    var json =                   { “store”: {                         “book”: [                           { “category”: “reference”,                                 “author”: “Nigel Rees”,                                 “title”: “Sayings of the Century”,                                 “price”: 8.95                           },                           { “category”: “fiction”,                                 “author”: “Evelyn Waugh”,                                 “title”: “Sword of Honour”,                                 “price”: 12.99                           },                           { “category”: “fiction”,                                 “author”: “Herman Melville”,                                 “title”: “Moby Dick”,                                 “isbn”: “0-553-21311-3”,                                 “price”: 8.99                           },                           { “category”: “fiction”,                                 “author”: “J. R. R. Tolkien”,                                 “title”: “The Lord of the Rings”,                                 “isbn”: “0-395-19395-8”,                                 “price”: 22.99                           }                         ],                         “bicycle”: {                           “color”: “red”,                           “price”: 19.95                         }                   }                 },        var test1 = jsonPath(json, “$.store.book[*].author”).toJSONString() + “\n>”;//精确检索        var test2 = jsonPath(json, “$..author”).toJSONString() + “\n”;        var test3 = jsonPath(json, “$.store.*”).toJSONString() + “\n”;        var test4 = jsonPath(json, “$.store..price”).toJSONString() + “\n”;        var test5 = jsonPath(json, “$..book[(@.length-1)]”).toJSONString() + “\n”;        var test6 = jsonPath(json, “$..book[-1:]”).toJSONString() + “\n”;        var test7 = jsonPath(json, “$..book[0,1]”).toJSONString() + “\n”;        var test8 = jsonPath(json, “$..book[:2]”).toJSONString() + “\n”;        var test9 = jsonPath(json, “$..book[?(@.isbn)]”).toJSONString() + “\n”;        var test10 = jsonPath(json, “$..book[?(@.price<10)]”).toJSONString() + “\n”;//条件建设        var test11 = jsonPath(json, “$..*”).toJSONString() + “\n”;//所有        var test12 = jsonPath(json, “$..book[?(@.title.indexOf(’the’) > -1)]”).toJSONString() + “\n”;  //模糊检索