
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义