最常用的使用場景:python實現接口測試中,從reponse中取值的操作,需要從多層路徑下取到某些值然后作為返回參數
#!/usr/bin/env python # coding = UTF-8 #Author:Lucky,time:2020/8/19 import jsonpath # 示例字典 D = { 'name': 'Ruo Data', 'web': { 'site': 'https://www.ruodata.com', 'basic': 'github', "test": [ { "test_syw":"hahha" }, { "test_syw":"hahha44" } ] }, 'major': 'python' } s = jsonpath.jsonpath(D,"$.web.test[1].test_syw") #第一種寫法(推薦),類似jmeter中的json Extractor的取值樣式 s1 = jsonpath.jsonpath(D,"$..test_syw") #第二種寫法,取值前有幾層,則寫幾個.. print (s) #返回的是一個列表 print(s1) #返回的是一個列表 print ("".join(s)) #列表轉為字符串