一、jsonpath是什么?
二、如何使用?
image.png
三、实践
1.$.filed1.field2
from jsonpath_ng import jsonpath,parse
json_str = {"data":{
"ecifId":"2022100400000103",
"operationId":"1628612966502549718749141715287189397281668389298609270",
"operationStatus":100,
"pinSetedFlag":"1",
"securityMethods":[]
},
"success":True}
expre_str = parse('$.data.operationStatus')
data = expre_str.find(json_str)
print("operationStatus:",data[0].value)
image.png
2.$.field1[*].id
获取列表中的所有
json_str1 = 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
}
}
}
expre_str = parse('$.store.book[*].price')
data = expre_str.find(json_str1)
prices = [d.value for d in data]
print(prices)
image.png
网友评论