栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA--基于阿里巴巴fastjson2 与 JsonPath以 jsonPath读取json内容示例及比较

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA--基于阿里巴巴fastjson2 与 JsonPath以 jsonPath读取json内容示例及比较

介绍 fastjson2

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依赖:

  
      com.alibaba.fastjson2
      fastjson2
      2.0.4
 
JsonPath

Jayway JsonPath: A Java DSL for reading JSON documents.
Jayway JsonPath is a Java port of Stefan Goessner JsonPath implementation.

源码地址

maven依赖


  com.jayway.jsonpath
  json-path
  2.7.0

以jsonPath读取示例 fastjson2示例
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直接返回实例类型
List authors = JsonPath.read(jsonStr, "$.store.book[*].author");
List prices = JsonPath.read(json, "$..price");

List> expensiveBooks = JsonPath
                .using(Configuration.defaultConfiguration())
                .parse(jsonStr)
                .read("$.store.book[?(@.price > 10)]", List.class);
System.out.println(expensiveBooks);

// fastjson2 只能返回Object
JSONReader parser = JSONReader.of(jsonStr);
Object obj = JSONPath.of(jsonPathStr).extract(parser);
  • 两者支持的jsonPath语法有差异,个人觉得JsonPath更为强大,但fastjson2也有逐步增强中
    • https://github.com/json-path/JsonPath
    • https://alibaba.github.io/fastjson2/jsonpath_cn
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/937499.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号