jsonpath的使用

用来提取数据的

案例一

    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())
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值