jsonpath 判断是否包含_JSONPath介绍

jsonpath 判断是否包含_JSONPath介绍/***@authoritguang*@create2017-12-1010:03**/@RunWith(SpringRunner.class)@SpringBootTest@Slf4jpublicclassJSONpathControllerTest{@Testpublicvoidtest(){Useruser=newUser(“itguang”,”123456″…

大家好,又见面了,我是你们的朋友全栈君。

jsonpath 判断是否包含_JSONPath介绍

/**

* @author itguang

* @create 2017-12-10 10:03

**/

@RunWith(SpringRunner.class)

@SpringBootTest

@Slf4j

public class JSONpathControllerTest {

@Test

public void test() {

User user = new User(“itguang”, “123456”, “123@qq.com”);

String username = (String) JSONPath.eval(user, “$.username”);

log.info(“$.username = {}”, username);

Entity entity = new Entity(123, user);

User user1 = (User) JSONPath.eval(entity, “$.value”);

log.info(“user={}”, user1.toString());

}

@Test

public void test2() {

User user = new User(“itguang”, “123456”, “123@qq.com”);

Entity entity = new Entity(123, user);

//判断entity中是否有 data

boolean contains = JSONPath.contains(entity, “$.data”);

Assert.assertTrue(contains);

//判断 entity.data.username 属性值是否为 itguang

boolean containsValue = JSONPath.containsValue(entity, “$.data.username”, “itguang”);

Assert.assertTrue(containsValue);

Assert.assertEquals(2, JSONPath.size(entity, “$”));

}

@Test

public void test3() {

List entities = new ArrayList();

entities.add(new Entity(“逻辑”));

entities.add(new Entity(“叶文杰”));

entities.add(new Entity(“程心”));

//返回集合中多个元素

List names = (List) JSONPath.eval(entities, “$.name”);

log.info(“返回集合中多个元素names={}”, names);

//返回下标 0 和 2 的元素

List result = (List) JSONPath.eval(entities, “[0,2]”);

log.info(“返回下标 0 和 2 的元素={}”, result);

// 返回下标从0到2的元素

List result2 = (List) JSONPath.eval(entities, “[0:2]”);

log.info(“返回下标从0到2的元素={}”, result2);

}

@Test

public void test4() {

List entities = new ArrayList();

entities.add(new Entity(1001, “逻辑”));

entities.add(new Entity(1002, “程心”));

entities.add(new Entity(1003, “叶文杰”));

entities.add(new Entity(1004, null));

//通过条件过滤,返回集合的子集

List result = (List) JSONPath.eval(entities, “[id in (1001)]”);

log.info(“通过条件过滤,返回集合的子集={}”, result);

}

/**

* 使用JSONPrase 解析JSON字符串或者Object对象

*

* read(String json, String path)//直接使用json字符串匹配

*

* eval(Object rootObject, String path) //直接使用 对象匹配

*

*

* {“store”:{“bicycle”:{“color”:”red”,”price”:19.95},”book”:[{“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”}]}}

*/

@Test

public void test5() {

String jsonStr = “{\n” +

” \”store\”: {\n” +

” \”bicycle\”: {\n” +

” \”color\”: \”red\”,\n” +

” \”price\”: 19.95\n” +

” },\n” +

” \”book\”: [\n” +

” {\n” +

” \”author\”: \”刘慈欣\”,\n” +

” \”price\”: 8.95,\n” +

” \”category\”: \”科幻\”,\n” +

” \”title\”: \”三体\”\n” +

” },\n” +

” {\n” +

” \”author\”: \”itguang\”,\n” +

” \”price\”: 12.99,\n” +

” \”category\”: \”编程语言\”,\n” +

” \”title\”: \”go语言实战\”\n” +

” }\n” +

” ]\n” +

” }\n” +

“}”;

JSONObject jsonObject = JSON.parseObject(jsonStr);

log.info(jsonObject.toString());

//得到所有的书

List books = (List) JSONPath.eval(jsonObject, “$.store.book”);

log.info(“books={}”, books);

//得到所有的书名

List titles = (List) JSONPath.eval(jsonObject, “$.store.book.title”);

log.info(“titles={}”, titles);

//第一本书title

String title = (String) JSONPath.read(jsonStr, “$.store.book[0].title”);

log.info(“title={}”, title);

//price大于10元的book

List list = (List) JSONPath.read(jsonStr, “$.store.book[price > 10]”);

log.info(“price大于10元的book={}”,list);

//price大于10元的title

List list2 =(List) JSONPath.read(jsonStr, “$.store.book[price > 10].title”);

log.info(“price大于10元的title={}”,list2);

//category(类别)为科幻的book

List list3 = (List) JSONPath.read(jsonStr,”$.store.book[category = ‘科幻’]”);

log.info(“category(类别)为科幻的book={}”,list3);

//bicycle的所有属性值

Collection values = (Collection) JSONPath.eval(jsonObject, “$.store.bicycle.*”);

log.info(“bicycle的所有属性值={}”,values);

//bicycle的color和price属性值

List read =(List) JSONPath.read(jsonStr, “$.store.bicycle[‘color’,’price’]”);

log.info(“bicycle的color和price属性值={}”,read);

}

}

jsonpath 判断是否包含_JSONPath介绍

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/150519.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • rpc协议详解

    rpc协议详解RPC是一种远程过程调用的协议,使用这种协议向另一台计算机上的程序请求服务,不需要了解底层网络技术的协议。HTTP是一种超文本传输协议。是WWW浏览器和WWW服务器之间的应用层通讯协议。TCP协议:传输控制协议,是一种面向连接的、可靠的、基于字节流的传输层通信协议。https://mp.weixin.qq.com/s?src=11&timestamp=1611734678&ver=2853&signature=igsdPz20ZUht*1IskaU0LxnFKvD6tD.

    2022年5月19日
    92
  • Tabnine 激活码破解方法【注册码】

    Tabnine 激活码破解方法【注册码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月20日
    38
  • 分页 模糊查询「建议收藏」

    分页 模糊查询「建议收藏」一、所有的web项目都会用到分页显示和模糊查询,对于有些人不知道该怎么写二、今天我用springMVC和myBatis写的分页和模糊分享给大家,不喜勿喷三、数据库是mysql四、其实写分页就是新建一个分页的类,定义页码每页数量共几页当前页数总数量五、判断多少页,获取总数量除以每页显示的数量,有余数+1页六、sql语句就是用…

    2022年5月11日
    44
  • 第一范式,第二范式,第三范式,BCNF范式理解

    第一范式,第二范式,第三范式,BCNF范式理解第一范式、第二范式、第三范式参考了https://www.zhihu.com/question/24696366https://www.cnblogs.com/lca1826/p/6601395.html基础知识实体:现实世界中客观存在并可以被区别的事物。比如“一个学生”、“一本书”、“一门课”等等。值得强调的是这里所说的“事物”不仅仅是看得见摸得着的“东西”,它也…

    2022年5月10日
    35
  • redis 如何查看版本

    redis 如何查看版本

    2021年10月27日
    50
  • MATLAB自带插值函数

    MATLAB自带插值函数一 interp11 函数简介 MATLAB 中的插值函数为 interp1 其调用格式为 yi interp1 x y xi method 其中 x y 为初始插值点 xi 为给定的插值点 yi 为在被插值点 xi 处的插值结果 method 表示采用的插值方法 MATLAB 提供的插值方法有几种 nearest 是最邻近插值 linear 线性插值 sp

    2025年6月8日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号