FastJson之JSONPath的使⽤
叙述
jsonpath,类似于xpath。都是通过⼀种字符串表达式,来快捷检索json⾥⾯的数据。在⾮常复杂的json结构中,对于⼀些获取和判断操作,不需要层层的去get。可以通过简洁的JsonPath表达式获取到结果。
解析
JSONPathfastjson怎么用
构造⽅法
public JSONPath(String path)
public JSONPath(String path, SerializeConfig serializeConfig, ParserConfig parserConfig)
静态属性/⽅法
public static Object eval(Object rootObject, String path)
public static Object extract(String json, String path, ParserConfig config, int features, optionFeatures)
public static Object extract(String json, String path)
* 根据path检索值
* extract,按需计算, 性能会更好
public static int size(Object rootObject, String path)
* 计算Size
* Map⾮空元素个数, 对象⾮空元素个数, Collection的Size, 数组的长度
* 其他⽆法求值返回-1
public static Set<?> keySet(Object rootObject, String path)
* 获取, Map的KeySet, 对象⾮空属性的名称
* 数组, Collection等不⽀持类型返回null
public static boolean contains(Object rootObject, String path)
* 是否包含, path中是否存在对象
public static boolean containsValue(Object rootObject, String path, Object value)
* 是否包含, path中是否存在指定值
* 如果是集合或者数组, 在集合中查value是否存在
public static void arrayAdd(Object rootObject, String path, values)
* 在数组或者集合中添加元素, 添加成功返回 true,失败返回 false
public static boolean set(Object rootObject, String path, Object value)
* 修改制定路径的值, 如果修改成功, 返回true, 否则返回false
public static boolean remove(Object root, String path)
* 删除指定path的元素, 删除成功返回 true,失败返回 false
public static JSONPath compile(String path)
* 编译⼀个jsonpath为对象
public static Object read(String json, String path)
* 从⼀个json字符串中, 根据指定的path读取为Json对象
public static Map<String, Object> paths(Object javaObject)
public static Map<String, Object> paths(Object javaObject, SerializeConfig config)
* 返回指定Java对象的属性的所有json访问path
看到这⾥也能明⽩,如果⼀个JsonPath需要多次重复使⽤的话,建议创建维护⼀个对象。⽽不是使⽤静态⽅法。
JsonPath⽀持的语法
Demo
public void test_entity() throws Exception {
Entity entity = new Entity(123, new Object());
Assert.Value(), JSONPath.eval(entity, "$.value"));
Assert.ains(entity, "$.value"));
Assert.ainsValue(entity, "$.id", 123));
Assert.ainsValue(entity, "$.value", Value()));
Assert.assertEquals(2, JSONPath.size(entity, "$"));
Assert.assertEquals(0, JSONPath.size(new Object[], "$"));
}
public static class Entity {
private Integer id;
private String name;
private Object value;
public Entity() {}
public Entity(Integer id, Object value) { this.id = id; this.value = value; }
public Entity(Integer id, String name) { this.id = id; this.name = name; }
public Entity(String name) { this.name = name; }
public Integer getId() { return id; }
public Object getValue() { return value; }
public String getName() { return name; }
public void setId(Integer id) { this.id = id; }
public void setName(String name) { this.name = name; }
public void setValue(Object value) { this.value = value; }
}
Demo - 读取数据
List<Entity> entities = new ArrayList<Entity>();
entities.add(new Entity("wenshao"));
entities.add(new Entity("ljw2083"));
List<String> names = (List<String>)JSONPath.eval(entities, "$.name"); // 返回enties的所有名称Assert.(0).getName(), (0));
Assert.(1).getName(), (1));
Demo2 - 返回集合中多个元素
List<Entity> entities = new ArrayList<Entity>();
entities.add(new Entity("wenshao"));
entities.add(new Entity("ljw2083"));
List<String> names = (List<String>)JSONPath.eval(entities, "$.name"); // 返回enties的所有名称Assert.(0).getName(), (0));
Assert.(1).getName(), (1));
Demo3 - 按范围返回集合的⼦集
List<Entity> entities = new ArrayList<Entity>();
entities.add(new Entity("wenshao"));
entities.add(new Entity("ljw2083"));
entities.add(new Entity("Yako"));
List<Entity> result = (List<Entity>)JSONPath.eval(entities, "[0:2]"); // 返回下标从0到2的元素
Assert.assertEquals(3, result.size());
Assert.(0), (0));
Assert.(1), (1));
Assert.(2), (1));
Demo4 - 通过条件过滤,返回集合的⼦集
List<Entity> entities = new ArrayList<Entity>();
entities.add(new Entity(1001, "ljw2083"));
entities.add(new Entity(1002, "wenshao"));
entities.add(new Entity(1003, "yakolee"));
entities.add(new Entity(1004, null));
List<Object> result = (List<Object>) JSONPath.eval(entities, "[id in (1001)]");
Assert.assertEquals(1, result.size());
Assert.(0), (0));
Demo5 - 根据属性值过滤条件判断是否返回对象,修改对象,数组属性添加元素
Entity entity = new Entity(1001, "ljw2083");
Assert.assertSame(entity , JSONPath.eval(entity, "[id = 1001]"));
Assert.assertNull(JSONPath.eval(entity, "[id = 1002]"));
JSONPath.set(entity, "id", 123456); //将id字段修改为123456
Assert.assertEquals(123456, Id().intValue());
JSONPath.set(entity, "value", new int[0]); //将value字段赋值为长度为0的数组
JSONPath.arrayAdd(entity, "value", 1, 2, 3); //将value字段的数组添加元素1,2,3
Demo6
Map root = Collections.singletonMap("company", //
Collections.singletonMap("departs", //
Arrays.asList( //
Collections.singletonMap("id",
1001), //
Collections.singletonMap("id",
1002), //
Collections.singletonMap("id", 1003) //
) //
));
List<Object> ids = (List<Object>) JSONPath.eval(root, "$..id");
assertEquals(3, ids.size());
assertEquals(1001, (0));
assertEquals(1002, (1));
assertEquals(1003, (2));
Demo - 7 KeySet
使⽤keySet抽取对象的属性名,null值属性的名字并不包含在keySet结果中,使⽤时需要注意
Entity e = new Entity();
e.setId(null);
e.setName("hello");
Map<String, Entity> map = Collections.singletonMap("e", e); Collection<String> result;
// id is null, excluded by keySet
result = (Collection<String>)JSONPath.eval(map, "$.e.keySet()"); assertEquals(1, result.size());
Assert.ains("name"));
e.setId(1L);
result = (Collection<String>)JSONPath.eval(map, "$.e.keySet()"); Assert.assertEquals(2, result.size());
Assert.ains("id")); // included
Assert.ains("name"));
// Same result
Assert.assertEquals(result, JSONPath.keySet(map, "$.e")); Assert.assertEquals(result, new JSONPath("$.e").keySet(map));

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。