JSON 信息抽取类库:JsonPath

jopen 10年前

JsonPath 对于 JSON 来说相当于 XPATH 对于 XML。这是一个简单的从文档中抽取指定信息的工具,提供多种语言实现版本,包括:Javascript, Java, Python 和 PHP。


给定以下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,          "isbn": "0-553-21311-3"        }      ],      "bicycle": {        "color": "red",        "price": 19.95      }    }  }

读取

所有作者:

List<String> authors = JsonPath.read(json, "$.store.book[*].author");

Author of first book in store:

String author = JsonPath.read(json, "$.store.book[1].author");

所有分类="reference"的书籍

List<Object> books = JsonPath.read(json, "$.store.book[?(@.category == 'reference')]");    List<Object> books = JsonPath.read(json, "$.store.book[?]", filter(where("category").is("reference")));

项目主页:http://www.open-open.com/lib/view/home/1385715175408