知识点
- 了解jsonpath模块的应用场景
- 掌握jsonpath模块的使用
- jsonpath模块的使用场景
在多层嵌套的复杂字典中,根据key和下表来批量提取value。 - 数据提取的方法
from jsonpath import jsonpath
ret = jsonpath(a,'json语法规则字符串')
- jsonpath语法规则
主要有三个
$ 根节点
. 子节点
.. 内部任意位置,子孙节点
- jsonpath练习
import json
import jsonpath
book_dict = '''{
"store":{
"book":[
{"category":"reference",
"author":"Nigel Rees",
"title":"Sayings of the Century",
"price":8.95
},
{"category":"fiction",
"author":"Eyelyn Waugh",
"title":"Sword of Honour",
"price":12.99
},
{"category":"reference",
"author":"Herman Melville",
"title":"Moby Dick",
"isbn":"0-553-21311-3",
"price":8.99
},
{"category":"reference",
"author":"J.R.R Tolkien",
"title":"The Lord of the Rings",
"price":22.99
}
],
"bicycle":[
{"color":"red",
"price":19.95
},
{"color":"blue",
"price":29.95
}
]
}
}'''
data = json.loads(book_dict)
print(jsonpath.jsonpath(data,"$..color"))
print(jsonpath.jsonpath(data,"$..price"))