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