【技术累积】【点】【java】【1】JSONPath

闲聊

以后周中每天一篇这种偏短的文章,周末就发长一点的文章,不然自己实在是懒,懒成了习惯了。。。

开始

首先需要明确的是,这里说的是阿里巴巴的fastjson包中的JSONPath,不是jsonPath,两者干的都是一件事儿,但用法什么的还是有较大不同。

可以大概浏览下这两篇文章:

其实说白了这东西就是做解析json串的,但是有一些比较好用的方法。

当然,其更加强大的事情是当做对象查询语言使用(上一篇引用中很详细的说了)

对于我而言,最吸引我的,是直接从json串的String中取值,判断等。

不多说,上代码:

    @Test
    public void testJsonPath(){
        String jsonText = "{\"name\":\"shitHappens\",\"age\":26,\"gender\":\"male\"}";
        JSONObject jsonObject = JSONObject.parseObject(jsonText);
        Object output = JSONPath.eval(jsonObject,"$.name");
        LOGGER.info("eval结果:{}", JSONObject.toJSONString(output));
    }

    @Test
    public void testJsonPath2(){
        String jsonText = "{\"names\":[{\"name\":\"shitHappens\",\"age\":26,\"gender\":\"male\"},{\"name\":\"shitHappens\",\"age\":2,\"gender\":\"male\"}]}";
        JSONObject jsonObject = JSONObject.parseObject(jsonText);
        Object output = JSONPath.eval(jsonObject,"$.names[1].age");
        List<Integer> ages = (List<Integer>) JSONPath.eval(jsonObject,"$..age");
        LOGGER.info("eval结果:{}", JSONObject.toJSONString(output));
    }

    @Test
    public void testJSONPath(){
        String jsonText = "{\"names\":[{\"name\":\"shitHappens\",\"age\":26,\"gender\":\"male\"},{\"name\":\"shitHappens\",\"age\":2,\"gender\":\"male\"}]}";
        JSONObject jsonObject = JSONObject.parseObject(jsonText);
        String path = "$.names[0].name";
//        JSONPath.arrayAdd(jsonObject,"");
        JSONPath jsonPath = JSONPath.compile(path);
        jsonPath.getPath();
        boolean contains = JSONPath.contains(jsonObject,path);
        boolean containsValue = JSONPath.containsValue(jsonObject,path,"shitHappens");
        int size = JSONPath.size(jsonObject,path);
        JSONPath.set(jsonObject,path,"dddd");
        LOGGER.info("json串:{}",jsonObject);
    }

几个点吧:

  • eval()方法,取值,关键是后面的路径的语法;
  • contains(),containsValue(),判断值的有无;
  • set(),设置值;

其实用起来很方便,关键是用的场景了吧,不过我还用的少,大概是那种String的json串,然后不是完全解析的场景吧。

最后还是学学别个的测试代码吧,用断言实现自动化

public void test_entity() throws Exception {
   Entity entity = new Entity(123, new Object());

  Assert.assertSame(entity.getValue(), JSONPath.eval(entity, "$.value")); 
  Assert.assertTrue(JSONPath.contains(entity, "$.value"));
  Assert.assertTrue(JSONPath.containsValue(entity, "$.id", 123));
  Assert.assertTrue(JSONPath.containsValue(entity, "$.value", entity.getValue())); 
  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; }
}

结束

东西还是要一搞好就记录下来的,毕竟,博客这些的目的就是记录,巩固当时的记忆,方便日后的快速回顾么,干!

  • JSONPath.eval()

猜你喜欢

转载自www.cnblogs.com/andy1202go/p/9291980.html