目录
1、运用re、json、jsonpath包解析json思路
(1)re:正则表达式,通过json的形式对症下药,写表达式去解析json;
(2)json: 通过json中的json.loads()方法,将str类型转为dict类型,运用python字典的数据结构特点去解析json;
(3)jsonpath:对比与json包,jsonpath可以根据路径去解析json,比较适合用来解析json中带“[ ]”的数据。
2、三种方式的json解析案例
#json数据样式,本文采用带[]的数据样式
{
"version": "version 1.0.12",
"result": {
"pages": 1314,
"data": [
{
"name": "大明",
"IDcard": "440588190001015688",
"address": "广东省广州市天河区正佳广场99楼520号",
},
{
"name": "二明",
"IDcard": "440588190012317456",
"address": "广东省广州市天河区天环广场88楼520号",
}
]
}
}
(1)运用re正则表达式解析json
import re
jsondata={
"version": "23231cimesfedkk",
"result": {
"pages": 1314,
"data": [
{
"name": "大明",
"IDcard": "440588190001015688",
"address": "广东省广州市天河区正佳广场99楼520号",
},
{
"name": "二明",
"IDcard": "440588190012317456",
"address": "广东省广州市天河区天环广场88楼520号",
}
]
}
}
if __name__ == '__main__':
name_list=re.findall(r"'name': '(\w*)'",str(jsondata))
IDcard_list=re.findall(r"'IDcard': '(\w{18})'", str(jsondata))#身份证18位数字和字母组合
address_list=re.findall(r"'address': '(\w*)'", str(jsondata))#地址
print(name_list)
print(IDcard_list)
print(address_list)
运行结果:
['大明', '二明']
['440588190001015688', '440588190012317456']
['广东省广州市天河区正佳广场99楼520号', '广东省广州市天河区天环广场88楼520号']
(2)运用字典的数据结构性质解析json
import json
jsondata={"version": "23231cimesfedkk","result": {"pages": 1314,"data": [{"name": "大明","IDcard": "440588190001015688","address": "广东省广州市天河区正佳广场99楼520号",},{"name": "二明","IDcard": "440588190012317456","address": "广东省广州市天河区天环广场88楼520号",}]}}
if __name__ == '__main__':
# 若传入的数据为str类型需要将它转成dict类型
# result = json.loads(jsondata)
jsondata = jsondata["result"]["data"]
resultdata = jsondata
namelist=[]
idcardlist=[]
addresslist=[]
for data in resultdata:
namelist.append(data['name'])
idcardlist.append(data['IDcard'])
addresslist.append(data['address'])
print(namelist)
print(idcardlist)
print(addresslist)
运行结果:
['大明', '二明']
['440588190001015688', '440588190012317456']
['广东省广州市天河区正佳广场99楼520号', '广东省广州市天河区天环广场88楼520号']
(3)运用jsonpath的路径解析json
import jsonpath
jsondata={"version": "23231cimesfedkk","result": {"pages": 1314,"data": [{"name": "大明","IDcard": "440588190001015688","address": "广东省广州市天河区正佳广场99楼520号",},{"name": "二明","IDcard": "440588190012317456","address": "广东省广州市天河区天环广场88楼520号",}]}}
if __name__ == '__main__':
namelist=[]
idcardlist=[]
addresslist=[]
#运用jsonpath.jsonpath(字典数据, 路径)
namelist=jsonpath.jsonpath(jsondata, '$..name')
idcardlist=jsonpath.jsonpath(jsondata, '$..IDcard')
addresslist=jsonpath.jsonpath(jsondata, '$..address')
print(namelist)
print(idcardlist)
print(addresslist)
运行结果:
['大明', '二明']
['440588190001015688', '440588190012317456']
['广东省广州市天河区正佳广场99楼520号', '广东省广州市天河区天环广场88楼520号']
3、附录:re正则表达式语法
文章出处登录后可见!
已经登录?立即刷新