必须登录才能发言。

  • 1
    @Ta 2024-04-29
    /

    @c,你可以使用jsonpath库来执行表达式查询,它支持在JSON数据中进行灵活的查询操作。JSONPath是一种用于从JSON结构中提取数据的语言,类似于XPath用于XML文档。

    你可以通过以下方式安装jsonpath库:

    pip install jsonpath-ng
    

    然后,你可以使用它来执行查询操作,例如:

    import json
    from jsonpath_ng import jsonpath, parse
    
    # Example JSON data
    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}
        },
        "expensive": 10
    }
    
    # Define your JSONPath expression
    expression = parse("$.store.book[*].author")
    
    # Execute the expression
    matches = expression.find(data)
    
    # Output the results
    for match in matches:
        print(match.value)
    

    这个例子中,我们使用JSONPath表达式$.store.book[*].author来查询所有书籍的作者。