1.首先我们需要安装第三方库,pip install jsonpath
    2.导入包

    1. from jsonpath import jsonpath

    image.png

    1. {
    2. "store": {
    3. "book": [
    4. { "category": "reference",
    5. "author": "Nigel Rees",
    6. "title": "Sayings of the Century",
    7. "price": 8.95
    8. },
    9. { "category": "fiction",
    10. "author": "Evelyn Waugh",
    11. "title": "Sword of Honour",
    12. "price": 12.99
    13. },
    14. { "category": "fiction",
    15. "author": "Herman Melville",
    16. "title": "Moby Dick",
    17. "isbn": "0-553-21311-3",
    18. "price": 8.99
    19. },
    20. { "category": "fiction",
    21. "author": "J. R. R. Tolkien",
    22. "title": "The Lord of the Rings",
    23. "isbn": "0-395-19395-8",
    24. "price": 22.99
    25. }
    26. ],
    27. "bicycle": {
    28. "color": "red",
    29. "price": 19.95
    30. }
    31. }
    32. }

    简单的案例:

    1. priceList = jsonpath(response,'$..book[*].price') # 获取book下所有的price
    2. color = jsonpath(response,'$.store.bicycle.color') # 获取颜色
    3. contain = jsonpath(response,'$..book[?(@.title)') # 获取book下面包含title的元素内容
    4. choice = jsonpath(response,'$..book[0,1].title') # 获取book内第一个和第二个的标题