当前位置:   article > 正文

FastJson 之 JSONPath的使用_fastjson path

fastjson path

转载自:https://springboot.io/t/topic/309

叙述

jsonpath,类似于xpath。都是通过一种字符串表达式,来快捷检索json里面的数据。在非常复杂的json结构中,对于一些获取和判断操作,不需要层层的去get。可以通过简洁的JsonPath表达式获取到结果。

解析

JSONPath

构造方法

  1. public JSONPath(String path)
  2. public JSONPath(String path, SerializeConfig serializeConfig, ParserConfig parserConfig)

静态属性/方法

  1. public static Object eval(Object rootObject, String path)
  2. public static Object extract(String json, String path, ParserConfig config, int features, Feature... optionFeatures)
  3. public static Object extract(String json, String path)
  4. * 根据path检索值
  5. * extract,按需计算, 性能会更好
  6. public static int size(Object rootObject, String path)
  7. * 计算Size
  8. * Map非空元素个数, 对象非空元素个数, Collection的Size, 数组的长度
  9. * 其他无法求值返回-1
  10. public static Set<?> keySet(Object rootObject, String path)
  11. * 获取, Map的KeySet, 对象非空属性的名称
  12. * 数组, Collection等不支持类型返回null
  13. public static boolean contains(Object rootObject, String path)
  14. * 是否包含, path中是否存在对象
  15. public static boolean containsValue(Object rootObject, String path, Object value)
  16. * 是否包含, path中是否存在指定值
  17. * 如果是集合或者数组, 在集合中查找value是否存在
  18. public static void arrayAdd(Object rootObject, String path, Object... values)
  19. * 在数组或者集合中添加元素, 添加成功返回 true,失败返回 false
  20. public static boolean set(Object rootObject, String path, Object value)
  21. * 修改制定路径的值, 如果修改成功, 返回true, 否则返回false
  22. public static boolean remove(Object root, String path)
  23. * 删除指定path的元素, 删除成功返回 true,失败返回 false
  24. public static JSONPath compile(String path)
  25. * 编译一个jsonpath为对象
  26. public static Object read(String json, String path)
  27. * 从一个json字符串中, 根据指定的path读取为Json对象
  28. public static Map<String, Object> paths(Object javaObject)
  29. public static Map<String, Object> paths(Object javaObject, SerializeConfig config)
  30. * 返回指定Java对象的属性的所有json访问path

看到这里也能明白,如果一个JsonPath需要多次重复使用的话,建议创建维护一个对象。而不是使用静态方法。

JsonPath支持的语法

Demo

  1. public void test_entity() throws Exception {
  2. Entity entity = new Entity(123, new Object());
  3. Assert.assertSame(entity.getValue(), JSONPath.eval(entity, "$.value"));
  4. Assert.assertTrue(JSONPath.contains(entity, "$.value"));
  5. Assert.assertTrue(JSONPath.containsValue(entity, "$.id", 123));
  6. Assert.assertTrue(JSONPath.containsValue(entity, "$.value", entity.getValue()));
  7. Assert.assertEquals(2, JSONPath.size(entity, "$"));
  8. Assert.assertEquals(0, JSONPath.size(new Object[], "$"));
  9. }
  10. public static class Entity {
  11. private Integer id;
  12. private String name;
  13. private Object value;
  14. public Entity() {}
  15. public Entity(Integer id, Object value) { this.id = id; this.value = value; }
  16. public Entity(Integer id, String name) { this.id = id; this.name = name; }
  17. public Entity(String name) { this.name = name; }
  18. public Integer getId() { return id; }
  19. public Object getValue() { return value; }
  20. public String getName() { return name; }
  21. public void setId(Integer id) { this.id = id; }
  22. public void setName(String name) { this.name = name; }
  23. public void setValue(Object value) { this.value = value; }
  24. }

Demo - 读取数据

  1. List<Entity> entities = new ArrayList<Entity>();
  2. entities.add(new Entity("wenshao"));
  3. entities.add(new Entity("ljw2083"));
  4. List<String> names = (List<String>)JSONPath.eval(entities, "$.name"); // 返回enties的所有名称
  5. Assert.assertSame(entities.get(0).getName(), names.get(0));
  6. Assert.assertSame(entities.get(1).getName(), names.get(1));

Demo2 - 返回集合中多个元素

  1. List<Entity> entities = new ArrayList<Entity>();
  2. entities.add(new Entity("wenshao"));
  3. entities.add(new Entity("ljw2083"));
  4. List<String> names = (List<String>)JSONPath.eval(entities, "$.name"); // 返回enties的所有名称
  5. Assert.assertSame(entities.get(0).getName(), names.get(0));
  6. Assert.assertSame(entities.get(1).getName(), names.get(1));

Demo3 - 按范围返回集合的子集

  1. List<Entity> entities = new ArrayList<Entity>();
  2. entities.add(new Entity("wenshao"));
  3. entities.add(new Entity("ljw2083"));
  4. entities.add(new Entity("Yako"));
  5. List<Entity> result = (List<Entity>)JSONPath.eval(entities, "[0:2]"); // 返回下标从0到2的元素
  6. Assert.assertEquals(3, result.size());
  7. Assert.assertSame(entities.get(0), result.get(0));
  8. Assert.assertSame(entities.get(1), result.get(1));
  9. Assert.assertSame(entities.get(2), result.get(1));

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

  1. List<Entity> entities = new ArrayList<Entity>();
  2. entities.add(new Entity(1001, "ljw2083"));
  3. entities.add(new Entity(1002, "wenshao"));
  4. entities.add(new Entity(1003, "yakolee"));
  5. entities.add(new Entity(1004, null));
  6. List<Object> result = (List<Object>) JSONPath.eval(entities, "[id in (1001)]");
  7. Assert.assertEquals(1, result.size());
  8. Assert.assertSame(entities.get(0), result.get(0));

Demo5 - 根据属性值过滤条件判断是否返回对象,修改对象,数组属性添加元素

  1. Entity entity = new Entity(1001, "ljw2083");
  2. Assert.assertSame(entity , JSONPath.eval(entity, "[id = 1001]"));
  3. Assert.assertNull(JSONPath.eval(entity, "[id = 1002]"));
  4. JSONPath.set(entity, "id", 123456); //将id字段修改为123456
  5. Assert.assertEquals(123456, entity.getId().intValue());
  6. JSONPath.set(entity, "value", new int[0]); //将value字段赋值为长度为0的数组
  7. JSONPath.arrayAdd(entity, "value", 1, 2, 3); //将value字段的数组添加元素1,2,3

Demo6

  1. Map root = Collections.singletonMap("company", //
  2. Collections.singletonMap("departs", //
  3. Arrays.asList( //
  4. Collections.singletonMap("id",
  5. 1001), //
  6. Collections.singletonMap("id",
  7. 1002), //
  8. Collections.singletonMap("id", 1003) //
  9. ) //
  10. ));
  11. List<Object> ids = (List<Object>) JSONPath.eval(root, "$..id");
  12. assertEquals(3, ids.size());
  13. assertEquals(1001, ids.get(0));
  14. assertEquals(1002, ids.get(1));
  15. assertEquals(1003, ids.get(2));

Demo - 7 KeySet

使用keySet抽取对象的属性名,null值属性的名字并不包含在keySet结果中,使用时需要注意

  1. Entity e = new Entity();
  2. e.setId(null);
  3. e.setName("hello");
  4. Map<String, Entity> map = Collections.singletonMap("e", e);
  5. Collection<String> result;
  6. // id is null, excluded by keySet
  7. result = (Collection<String>)JSONPath.eval(map, "$.e.keySet()");
  8. assertEquals(1, result.size());
  9. Assert.assertTrue(result.contains("name"));
  10. e.setId(1L);
  11. result = (Collection<String>)JSONPath.eval(map, "$.e.keySet()");
  12. Assert.assertEquals(2, result.size());
  13. Assert.assertTrue(result.contains("id")); // included
  14. Assert.assertTrue(result.contains("name"));
  15. // Same result
  16. Assert.assertEquals(result, JSONPath.keySet(map, "$.e"));
  17. Assert.assertEquals(result, new JSONPath("$.e").keySet(map));

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/991126
推荐阅读
相关标签
  

闽ICP备14008679号