jsonpath模块
$ 表示根节点
. 表示子节点
.. 表示内部任意位置
1,如何通过jsonpath取json里面的值
导入jsonpath模块:from jsonpath import jsonpath
dict = {"key1":{"key2":{"key3":{"key4":{"key5":"request"}}}}}
# 1,普通的提取方式
print(dict["key1"]["key2"]["key3"]["key4"]["key5"])
# 2.jsonpath 提取,提取的值是list
print(jsonpath(dict,"$..key5")[0])
=======================================================================
2,# jsonpath 提取到的值是存放在一个list中
print(jsonpath(book_dict,"$..color")) # 取根节点任意位置的color的值
print(jsonpath(book_dict,"$..title")) # 取根节点任意位置的title的值
print(jsonpath(book_dict,"$..book[0].title")) # 取根节点任意位置下book第一个下标的title的值
print(jsonpath(book_dict,"$.store.book[0].title")) # 取根节点下子节点store下book的第一个下标的title的值
print(jsonpath(book_dict,"$.store.book[1].price")[0]) # 根节点下子节点store下book的第二个下标的price的值
3,通过jsonpath取值
# 1,提取所有的城市name
# 2,提取名称是g开头的城市
# 3,提取id=906的name
print(jsonpath(data,"$..name"))
print(jsonpath(data,"$..G..name"))
print(jsonpath(data,"$..Z.[-1:].name"))
标签:知识点,..,节点,jsonpath,dict,模块,print,book From: https://www.cnblogs.com/YouJeffrey/p/17768564.html