jsonpath

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

四、表达式使用示例:

1、数据准备

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
}
},
“expensive”: 10
}
在这里插入图片描述
五、在JAVA代码中使用

1、pom.xml文件中引入json-path的依赖

xml代码

< dependency>
< groupId>com.jayway.jsonpath</ groupId>
< artifactId>json-path< /artifactId>
< version>2.4.0< /version>
</ dependency>

2、编写代码

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Map;

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.spi.json.JsonProvider;

public class JsonPathTest {
public static void main(String[] args) {
String json = readJson();
Configuration configuration = Configuration.defaultConfiguration();
configuration = configuration.addOptions(//
Option.DEFAULT_PATH_LEAF_TO_NULL, // 如果路径不存在则返回null,而不要抛出PathNotFoundException
Option.SUPPRESS_EXCEPTIONS // 抑制异常的抛出,当设置了Option.ALWAYS_RETURN_LIST时返回[],否则返回null
);
// 如果项目中存在gson则此处可以new 一个GsonJsonProvider,那么返回对象
// configuration.jsonProvider(new GsonJsonProvider());
JsonProvider jsonProvider = configuration.jsonProvider();
/**
* 此处预先解析json,默认请情下JsonPath.read方法每掉一次都会重新解析json,此处预先解析好就不用每次都进行解析
*/
Object document = jsonProvider.parse(json);
// 1.获取所有book中的author
List authors = JsonPath.read(document, “ . s t o r e . b o o k [ ∗ ] . a u t h o r &quot; ) ; S y s t e m . o u t . p r i n t l n ( a u t h o r s ) ; / / 2. 获 取 价 格 比 .store.book[*].author&quot;); System.out.println(authors); // 2.获取价格比 .store.book[].author");System.out.println(authors);//2..expensive大的书籍
List<Map<String, Object>> books = JsonPath.read(document, “$.store.book[?(@.price > . e x p e n s i v e ) ] &quot; ) ; S y s t e m . o u t . p r i n t l n ( b o o k s ) ; / / 3. 输 出 一 个 不 存 在 的 表 达 式 , 因 为 存 在 这 个 选 项 的 配 置 , 所 有 返 回 的 是 O p t i o n . D E F A U L T P A T H L E A F T O N U L L O b j e c t n o t E x i s t s V a l u e = J s o n P a t h . u s i n g ( c o n f i g u r a t i o n ) . p a r s e ( d o c u m e n t ) . r e a d ( &quot; .expensive)]&quot;); System.out.println(books); // 3.输出一个不存在的表达式,因为存在这个选项的配置,所有返回的是Option.DEFAULT_PATH_LEAF_TO_NULL Object notExistsValue = JsonPath.using(configuration).parse(document).read(&quot; .expensive)]");System.out.println(books);//3.,Option.DEFAULTPATHLEAFTONULLObjectnotExistsValue=JsonPath.using(configuration).parse(document).read(".not-exists-path.path”);
System.out.println(notExistsValue);
// 4.存在多级属性缺失
Object object = JsonPath.using(configuration).parse(document).read(”$.not-exists.path");
System.out.println(object);
}

public static String readJson() {  
    StringBuilder builder = new StringBuilder();  
    try (InputStream is = JsonPathTest.class.getResourceAsStream("data.json"); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr);{  
        String line;  
        while (null != (line = br.readLine())) {  
            builder.append(line);  
        }  
    } catch (Exception e) {  
        e.printStackTrace();  
    }  
    return builder.toString();  
}  

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值