博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FastJson中JSONPath的应用
阅读量:4182 次
发布时间:2019-05-26

本文共 2838 字,大约阅读时间需要 9 分钟。

JSONPath是FastJson的一个类 先解析JSON数据为JSONObject,然后就能直接使用JSONPath了。 (fastjson在1.2.0之后就支持jsonpath了)

package com.xiaobu.note.json.fastjson;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.alibaba.fastjson.JSONPath;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2020/3/13 8:23 * @description */public class FastJsonDemo1 {
public static void main(String[] args) {
String jsonStr = "{ \"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}}}"; // 先解析JSON数据为JSONObject,然后就能直接使用JSONPath了。 JSONObject jsonObject = JSON.parseObject(jsonStr); System.out.println("book数目:"+ JSONPath.eval(jsonObject, "$.store.book.size()") ); System.out.println("第一本书的title:"+JSONPath.eval(jsonObject,"$.store.book[0].title")); System.out.println("第一本书的category和author:"+JSONPath.eval(jsonObject,"$.store.book[0]['category','author']")); System.out.println("price>10的书:"+JSONPath.eval(jsonObject,"$.store.book[price>10]")); System.out.println("price>8的书的标题:"+JSONPath.eval(jsonObject,"$.store.book[price>8]")); System.out.println("price>7的书:"+JSONPath.eval(jsonObject,"$.store.book[price>7]")); System.out.println("price>7的书的标题:"+JSONPath.eval(jsonObject,"$.store.book[price>7].title")); //不带单引号会出现Exception in thread "main" java.lang.UnsupportedOperationException 异常 System.out.println("书的标题为Sayings of the Century:"+JSONPath.eval(jsonObject,"$.store.book[title='Sayings of the Century']")); System.out.println("bicycle的所有属性:"+JSONPath.eval(jsonObject,"$.store.bicycle.*")); System.out.println("bicycle:"+JSONPath.eval(jsonObject,"$.store.bicycle")); }}

结果:

book数目:2第一本书的title:Sayings of the Century第一本书的category和author:[reference, Nigel Rees]price>10的书:[{"author":"Evelyn Waugh","price":12.99,"isbn":"0-553-21311-3","category":"fiction","title":"Sword of Honour"}]price>8的书的标题:[{"author":"Evelyn Waugh","price":12.99,"isbn":"0-553-21311-3","category":"fiction","title":"Sword of Honour"}]price>7的书:[{"author":"Nigel Rees","price":8.95,"category":"reference","title":"Sayings of the Century"}, {"author":"Evelyn Waugh","price":12.99,"isbn":"0-553-21311-3","category":"fiction","title":"Sword of Honour"}]price>7的书的标题:[Sayings of the Century, Sword of Honour]书的标题为Sayings of the Century:[{"author":"Nigel Rees","price":8.95,"category":"reference","title":"Sayings of the Century"}]bicycle的所有属性:[red, 19.95]bicycle:{"color":"red","price":19.95}

个人理解:不适用于需要很多的json属性的业务场景。

转载地址:http://awgai.baihongyu.com/

你可能感兴趣的文章
andorid里关于wifi的分析
查看>>
Hibernate和IBatis对比
查看>>
Spring MVC 教程,快速入门,深入分析
查看>>
Android 的source (需安装 git repo)
查看>>
Ubuntu Navicat for MySQL安装以及破解方案
查看>>
java多线程中的join方法详解
查看>>
在C++中如何实现模板函数的外部调用
查看>>
HTML5学习之——HTML 5 应用程序缓存
查看>>
HTML5学习之——HTML 5 服务器发送事件
查看>>
SVG学习之——HTML 页面中的 SVG
查看>>
SVG 滤镜学习之——SVG 滤镜
查看>>
mysql中用命令行复制表结构的方法
查看>>
hbase shell出现ERROR: org.apache.hadoop.hbase.ipc.ServerNotRunningYetException
查看>>
让代码变得更优雅-Lombok
查看>>
解决Rhythmbox乱码
查看>>
豆瓣爱问共享资料插件发布啦
查看>>
kermit的安装和配置
查看>>
vim 配置
查看>>
openocd zylin
查看>>
进程创建时文件系统处理
查看>>