FASTJSON v2是FASTJSON项目的重要升级,目标是为下一个十年提供一个高性能的JSON库。通过同一套API,
- 支持JSON/JSONB两种协议,JSONPath 是一等公民。
- 支持全量解析和部分解析。
- 支持Java服务端、客户端Android、大数据场景。
- 支持Kotlin https://alibaba.github.io/fastjson2/kotlin_cn
- 支持JSON Schema https://alibaba.github.io/fastjson2/json_schema_cn
- 支持Android (2.0.4.android)
- 支持Graal Native-Image (2.0.4.graal)
源码地址
maven依赖:
JsonPathcom.alibaba.fastjson2 fastjson2 2.0.4
Jayway JsonPath: A Java DSL for reading JSON documents.
Jayway JsonPath is a Java port of Stefan Goessner JsonPath implementation.
源码地址
maven依赖
以jsonPath读取示例 fastjson2示例com.jayway.jsonpath json-path 2.7.0
import com.alibaba.fastjson2.JSONPath;
import com.alibaba.fastjson2.JSONReader;
public class JSONTest {
private static int num = 1;
private static void readJSONPathContent(String jsonStr, String jsonPathStr) {
JSONReader parser = JSONReader.of(jsonStr);
Object obj = JSONPath.of(jsonPathStr).extract(parser);
System.out.println(String.format("第%s次,jsonPath: %s, n结果是:%sn==================",
num,jsonPathStr, obj));
num++;
}
public static void main(String[] args) {
String jsonStr = "{ "store": {n" +
" "book": [n" +
" { "category": "reference",n" +
" "author": "雨果",n" +
" "title": "悲惨世界",n" +
" "price": 8.95n" +
" },n" +
" { "category": "fiction",n" +
" "author": "海明威",n" +
" "title": "老人与海",n" +
" "price": 12.99,n" +
" "isbn": "0-553-21311-3"n" +
" }n" +
" ],n" +
" "bicycle": {n" +
" "color": "red",n" +
" "price": 19.95n" +
" }n" +
" }n" +
"}";
//输出book[0]的author值
readJSONPathContent(jsonStr, "$.store.book[0].author");
//输出全部author的值
readJSONPathContent(jsonStr, "$.store.book[*].author");
//输出book[*]中category == 'reference'的book
readJSONPathContent(jsonStr, "$.store.book[?(@.category == 'reference')]");
//输出book[*]中category == 'reference' 或者 price >10的book
readJSONPathContent(jsonStr, "$.store.book[?(@.category == 'reference' || @.price>10)]");
//输出book[*]中category == 'reference'的book的author
readJSONPathContent(jsonStr, "$.store.book[?(@.category == 'reference')].author");
//输出book[*]中price>10的book
readJSONPathContent(jsonStr, "$.store.book[?(@.price>10)]");
//输出book[*]中含有isbn元素的book
readJSONPathContent(jsonStr, "$.store.book[?(@.isbn)]");
//输出该json中所有price的值
readJSONPathContent(jsonStr, "$..price");
//输出该json中所有title的值
readJSONPathContent(jsonStr, "$..title");
//输出该json中book 0,1的值
readJSONPathContent(jsonStr, "$..book[0,1]");
//输出该json中book 0,1的值
readJSONPathContent(jsonStr, "$..book[-2:]");
//可以提前编辑一个路径,并多次使用它
readJSONPathContent(jsonStr, "$.store.book[*]");
}
JsonPath示例
只需要将readJSONPathContent方法调整为如下:
private static void readJSONPathContent(String jsonStr, String jsonPathStr) {
Object obj = JsonPath.read(jsonStr, jsonPathStr);
System.out.println(String.format("第%s次,jsonPath: %s, n结果是:%sn==================",
num,jsonPathStr, obj));
num++;
}
区别
- JsonPath可以直接具体的实例类型,但fastjson2只能返回Object对象
// JsonPath直接返回实例类型 Listauthors = JsonPath.read(jsonStr, "$.store.book[*].author"); List prices = JsonPath.read(json, "$..price"); List
- 两者支持的jsonPath语法有差异,个人觉得JsonPath更为强大,但fastjson2也有逐步增强中
- https://github.com/json-path/JsonPath
- https://alibaba.github.io/fastjson2/jsonpath_cn



