用来提取数据的
案例一
data = {"store": {
"book": [
{"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
#相当于框架已封装的方法get_json_value_by_key()
print("获取所有作者列表")
print(jsonpath.jsonpath(data,"$.store.book[*].author"))
print("获取所有作者列表")
print(jsonpath.jsonpath(data, "$..author"))
print("获取所有store")
print(jsonpath.jsonpath(data, "$.store.*"))
print("获取所有price列表")
print(jsonpath.jsonpath(data,"$.store..price"))
print("获取第三本书")
print(jsonpath.jsonpath(data, "$..book[2]"))
print("获取最后一本书")
print(jsonpath.jsonpath(data, "$..book[-1:]"))
#相当于框架已封装的方法get_item_by_key_value()
print("条件筛选-获取书(返回有isbn值的数据)")
print(jsonpath.jsonpath(data, "$..book[?(@.isbn)]"))
#"$..book[?(@.isbn==XXX)]"XXX条件
/*
结果
获取所有作者列表
['Nigel Rees', 'Evelyn Waugh', 'Herman Melville', 'J. R. R. Tolkien']
获取所有作者列表
['Nigel Rees', 'Evelyn Waugh', 'Herman Melville', 'J. R. R. Tolkien']
获取所有store
[[{'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}, {'category': 'fiction', 'author': 'Evelyn Waugh', 'title': 'Sword of Honour', 'price': 12.99}, {'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}, {'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}], {'color': 'red', 'price': 19.95}]
获取所有price列表
[8.95, 12.99, 8.99, 22.99, 19.95]
获取第三本书
[{'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}]
获取最后一本书
[{'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]
条件筛选-获取书
[{'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}, {'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]
*/
案例二
data = {
"jsonrpc": "2.0", "id": "6505336973521913828", "method": "topup",
"params": [
{
"quantity": 1, "orderId": "6505336973461307401", "signature": "e58c03553e55dff76f7f8ec967ab2ce31a87aa090eb3cac3f02ff2e19df3d0c3",
"price": {"currency": "IDR", "amount": 79000.0}, "merchant": "sultans", "sku": "1750",
"user": {"userId": "10140003842", "zoneId": ""}, "txnId": "6505336973521913828", "paymentChannelId": 227
}
]}
key_list=["id","jsonrpc","method","merchant","txnId","orderId","userId","zoneId","currency","amount","sku","quantity","paymentChannelId","hcuj"]
msg=""
for key_vlue in key_list:
# print(jsonpath.jsonpath(data,"$..{}".format(key_vlue)))
data_str=jsonpath.jsonpath(data,"$..{}".format(key_vlue))
if data_str is False:
continue
if isinstance(data_str[0],str) is False:
data_str[0]=str(data_str[0])
msg+=data_str[0]
output = key.encode('utf-8')
message = msg.encode('utf-8')
print(hmac.new(output, message, hashlib.sha256).hexdigest())