jsonpath 类的用法

复制代码
 1 import json
 2 
 3 #字典 ==> json
 4 test_dict = {"key1":"val1","key2":None,"key3":True,"key4":False}
 5 new_json = json.dumps(test_dict)
 6 print(type(new_json),new_json)     #<class 'str'> {"key1": "val1", "key2": null, "key3": true, "key4": false}
 7 """
 8 字典与json之间存在差异:
 9 dict = {"key1": "val1", "key2": None, "key3": True, "key4": False}
10 json = {"key1": "val1", "key2": null, "key3": true, "key4": false}
11 """
12 
13 #json ==> 字典
14 finally_dict = json.loads(new_json)
15 print(type(finally_dict),finally_dict)    #<class 'dict'> {'key1': 'val1', 'key2': None, 'key3': True, 'key4': False}
16 
17 from jsonpath import jsonpath
18 from pprint import pprint
19 from Demo2 import test_dic
20 #result(list) = jsonpath(dic,表达式)
21 #result = jsonpath(test_dic,"$[user_info]")
22 #result = jsonpath(test_dic,"$[user_info][China]")
23 #result = jsonpath(test_dic,"$..China")    #模糊递归搜索 $..
24 #result = jsonpath(test_dic,"$..China[0,1]")    #索引搜索
25 #result = jsonpath(test_dic,"$..China[2:4]")    #切片搜索
26 #result = jsonpath(test_dic,"$..China[[name,age,heighet]]")    #获取指定字段
27 #result = jsonpath(test_dic,"$..China.[name,age,heighet]")     #获取指定字段
28 result = jsonpath(test_dic,"$..user_info.[?(@.age<25 && @.sex=='女')]")      #过滤表达式搜索   && 和 ||
29 
30 pprint(result)
复制代码

 参考文章:http://testingpai.com/article/1595507145193

http://testingpai.com/article/1628759818085

posted @   Chaman囍  阅读(38)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示