newalan

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

原文:史上最详细的jsonpath教程,它来了!!!

https://www.jianshu.com/p/3f5b9cc88bde

例子:

{ "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
    }
  }
}

// 获取作者的名称
import jsonpath
author = jsonpath.jsonpath(data_json, '$.store.book[*].author')
print(author)

// 或者
author = jsonpath.jsonpath(data_json, '$..author')

// 获取第三本书的价格
third_book_price = jsonpath.jsonpath(data_json, '$.store.book[2].price')

// 将含有isbn编号的书籍过滤出来
isbn_book = jsonpath.jsonpath(data_json, '$..book[?(@.isbn)]')

// 将价格小于10元的书过滤出来
book = jsonpath.jsonpath(data_json, '$..book[?(@.price<10)]')

 

 

 
posted on 2024-12-07 11:24  newalan  阅读(11)  评论(0)    收藏  举报