【技術累積】【點】【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()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM