目录
一、json
作用:
主要用来处理python中的json
使用:
import json
# 原始变量为dict类型
test_dict = {"key1": "val1", "key2": None, "key3": "True", "key4": "False"}
print(type(test_dict))
# <class 'dict'>
# 转成json_str格式
test_json = json.dumps(test_dict)
print(type(test_json), test_json)
# <class 'str'> {"key1": "val1", "key2": null, "key3": "True", "key4": "False"}
# 将json_str格式转成字典,即python对象
test_dict1 = json.loads(test_json)
print(type(test_dict1), test_dict1)
# <class 'dict'> {'key1': 'val1', 'key2': None, 'key3': 'True', 'key4': 'False'}
二、jsonpath
作用:
从接口响应结果中提取数据,用与其他接口
只能处理json格式的数据(python中只能处理dict)
安装:
pip install jsonpath==0.82
使用:
示例代码:
user_info = {"technology":
{"python": [
{"name": "张三",
"sex": "男",
"age": 30,
"height": 175,
"info": "python工程师"
},
{"name": "李四",
"sex": "男",
"age": 28,
"height": 185,
"info": "python工程师"
},
{"name": "小花",
"sex": "女",
"age": 18,
"height": 170,
"info": "python工程师"
},
{"name": "王五",
"sex": "男",
"age": 28,
"height": 185,
"info": "python开发工程师"
},
{"name": "赵六",
"sex": "男",
"age": 28,

本文介绍了JSON的基本概念及其在Python中的应用,并详细讲解了JSONPath的安装与使用方法,包括如何提取复杂JSON结构中的特定数据。
最低0.47元/天 解锁文章
721

被折叠的 条评论
为什么被折叠?



