JsonPath

作者: pandak | 来源:发表于2018-06-04 17:35 被阅读0次
    jsonpath.png

    JsonPath的用法示例:

    import json
    
    from jsonpath import jsonpath
    
    if __name__ == '__main__':
        dict = {"class": {"students": [{"student_id": "1", "name": "bob", "sex": "male", "age": 6},
                                       {"student_id": "2", "name": "amy", "sex": "female", "age": 6},
                                       {"student_id": "3", "name": "pery", "sex": "male", "age": 5}], 
                                       "teachers": {"teacher_id": "1", "name": "anne", "sex": "female", "age": 32}}}
    
        # 获取根节点下的任意name属性的值
        print(jsonpath(dict, '$..name'))  # 输出 ['bob', 'amy', 'pery', 'anne']
    
        # 获取teachers节点
        print(jsonpath(dict, '$.class.teachers'))  # 输出 [{'teacher_id': '1', 'name': 'anne', 'sex': 'female', 'age': 32}]
    
        # 获取第一个students数据
        print(jsonpath(dict, '$..students[0]'))  # 输出  [{'student_id': '1', 'name': 'bob', 'sex': 'male', 'age': 6}]
    
        # 获取students的第一条数据的name属性
        print(jsonpath(dict, '$..students[0].name'))  # 输出 ['bob']
    
        # 获取students的0,1条数据
        print(jsonpath(dict, '$..students[0,1]'))   # 输出 [{'student_id': '1', 'name': 'bob', 'sex': 'male', 'age': 6}, {'student_id': '2', 'name': 'amy', 'sex': 'female', 'age': 6}]
        print(jsonpath(dict, '$..students[:2]'))    # 输出 [{'student_id': '1', 'name': 'bob', 'sex': 'male', 'age': 6}, {'student_id': '2', 'name': 'amy', 'sex': 'female', 'age': 6}]
    
        # 获取students的最后一条数据
        print(jsonpath(dict, '$..students[-1:]'))  # 输出 [{'student_id': '3', 'name': 'pery', 'sex': 'male', 'age': 5}]
    
    

    相关文章

      网友评论

          本文标题:JsonPath

          本文链接:https://www.haomeiwen.com/subject/yapysftx.html