Interface associations can usually use regular expressions to extract the required data , But for the JSON This simplicity 、 Clear hierarchy 、 Lightweight data interaction format , Using regular is a bit like killing a chicken with an ox knife ( Yes , Because I'm not good at writing regular expressions ), We need to be simpler 、 Direct extraction JSON The way of data .
install :pip install jsonpath
Usage mode :jsonpath.jsonpath( A dictionary object ,jsonpath expression )
Return value : list .
"""
import jsonpath
resp = {
"code": 0,
"msg": "OK",
"data": {
"id": 200110,
"leave_amount": 4000.0,
"mobile_phone": "135000000002",
"reg_name": " Good name ",
"reg_time": "2020-06-29 11:52:20.0",
"type": 1,
"token_info": {
"token_type": "Bearer",
"expires_in": "2020-07-06 21:48:53",
"token": "7fJjnvS6xVw7_hYTe7eVIyxj3W2Oj7SlwR8dDZBc1T59U2ngRROXyFjx_Q"
}
},
"copyright": "Copyright"
}
# $.code
# Parameters : first : A dictionary object the second :jsonpath expression
# Return value : list . Store matches to all values .
# res = jsonpath.jsonpath(resp,"$.data.token_info.token")
# print(res)
a = jsonpath.jsonpath(resp,"$.copyright")[0]
print(a)
The first 44 A video explaining