Python3 JSON 数据解析
JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式。
如果你还不了解 JSON,可以先阅读我们的 JSON 教程。
Python3 中可以使用 json 模块来对 JSON 数据进行编解码,它包含了两个函数:
- json.dumps(): 对数据进行编码。
- json.loads(): 对数据进行解码。
对接端python值:
{'companyId': 39978, 'publisher': 82752, 'appCvConfigId': 6892, 'channels': [{'channel': 2, 'salaryTop': 12, 'salaryBottom': 10, 'occupations': None, 'count': 0, 'addresses': [], 'salaryMonth': 13, 'departmentName': 'Parks, Experiences and Products', 'feature': None, 'salaryDiscuss': True}, {'isSchool': False, 'occupation': None, 'dailySalaryTop': 200, 'dailySalaryBottom': 100, 'dayPerWeek': 3, 'monthNum': 6, 'effectiveTime': '2022-12-21', 'language': 'chinese', 'address': '上海', 'chance': 'notsure', 'features': ['电影票', '迪士尼特色福利']}, {'channel': 6, 'salaryTop': 0, 'salaryBottom': 0, 'occupations': [], 'count': 0, 'addresses': [], 'companyName': None, 'departmentName': None, 'feature': None, 'startDate': '2021-12-21', 'endDate': '2022-01-20', 'languages': 11, 'jobClassId': '上海迪士尼度假区', 'jobOrder': 1, 'linkmanPhone': '021-20603105', 'linkman': '上海迪士尼度假区招聘中心', 'occupation': None}], 'positionStrategy': '1', 'accountAbilities': 'a\nc', 'jobNumber': '60302BR', 'title': 'Host/Hostess, FL PT Test', 'cities': [{'type': 'text', 'value': 'Shanghai'}], 'department': 'Parks, Experiences and Products', 'occupation': 'Hotel', 'custom': '一线运营岗位', 'candidateSource': 0, 'employmentType': 1, 'additionAccountability': 'c', 'requirement': 'd', 'additionalInformation': 'Postingchannel:wechat', 'additionRequirement': '', 'education': '', 'processId': 124, 'emailNotice': 1, 'source': 9, 'sourceId': 77, 'createDepartment': True, 'showDepartment': True, 'enableCustom': True, 'enableOccupation': True, 'createCustom': True}
json序列化后传给基础服务java的值:
在 json 的编解码过程中,Python 的原始类型与 json 类型会相互转换,具体的转化对照如下:
Python 编码为 JSON 类型转换对应表:
| Python | JSON |
|---|---|
| dict | object |
| list, tuple | array |
| str | string |
| int, float, int- & float-derived Enums | number |
| True | true |
| False | false |
| None | null |
JSON 解码为 Python 类型转换对应表:
| JSON | Python |
|---|---|
| object | dict |
| array | list |
| string | str |
| number (int) | int |
| number (real) | float |
| true | True |
| false | False |
| null | None |
对接端python值:
{'companyId': 39978, 'publisher': 82752, 'appCvConfigId': 6892, 'channels': [{'channel': 2, 'salaryTop': 12, 'salaryBottom': 10, 'occupations': None, 'count': 0, 'addresses': [], 'salaryMonth': 13, 'departmentName': 'Parks, Experiences and Products', 'feature': None, 'salaryDiscuss': True}, {'isSchool': False, 'occupation': None, 'dailySalaryTop': 200, 'dailySalaryBottom': 100, 'dayPerWeek': 3, 'monthNum': 6, 'effectiveTime': '2022-12-21', 'language': 'chinese', 'address': '上海', 'chance': 'notsure', 'features': ['电影票', '迪士尼特色福利']}, {'channel': 6, 'salaryTop': 0, 'salaryBottom': 0, 'occupations': [], 'count': 0, 'addresses': [], 'companyName': None, 'departmentName': None, 'feature': None, 'startDate': '2021-12-21', 'endDate': '2022-01-20', 'languages': 11, 'jobClassId': '上海迪士尼度假区', 'jobOrder': 1, 'linkmanPhone': '021-20603105', 'linkman': '上海迪士尼度假区招聘中心', 'occupation': None}], 'positionStrategy': '1', 'accountAbilities': 'a\nc', 'jobNumber': '60302BR', 'title': 'Host/Hostess, FL PT Test', 'cities': [{'type': 'text', 'value': 'Shanghai'}], 'department': 'Parks, Experiences and Products', 'occupation': 'Hotel', 'custom': '一线运营岗位', 'candidateSource': 0, 'employmentType': 1, 'additionAccountability': 'c', 'requirement': 'd', 'additionalInformation': 'Postingchannel:wechat', 'additionRequirement': '', 'education': '', 'processId': 124, 'emailNotice': 1, 'source': 9, 'sourceId': 77, 'createDepartment': True, 'showDepartment': True, 'enableCustom': True, 'enableOccupation': True, 'createCustom': True}
json序列化后传给基础服务java的值(请求参数):
DEBUG: 调用v5/positions 保存职位信息为{
"companyId": 39978,
"publisher": 82752,
"appCvConfigId": 6892,
"channels": [
{
"channel": 14,
"isSchool": false,
"occupation": "酒店旅游",
"dailySalaryTop": 200,
"dailySalaryBottom": 100,
"dayPerWeek": 3,
"monthNum": 6,
"effectiveTime": "2022-12-21",
"language": "chinese",
"address": "上海",
"chance": "notsure",
"features": [
"电影票",
"迪士尼特色福利"
]
},
{
"channel": 2,
"salaryTop": 12,
"salaryBottom": 10,
"occupations": null,
"count": 0,
"addresses": [],
"salaryMonth": 13,
"departmentName": "Parks, Experiences and Products",
"feature": null,
"salaryDiscuss": true
},
{
"channel": 6,
"salaryTop": 0,
"salaryBottom": 0,
"occupations": [],
"count": 0,
"addresses": [],
"companyName": null,
"departmentName": null,
"feature": null,
"startDate": "2021-12-21",
"endDate": "2022-01-20",
"languages": 11,
"jobClassId": "上海迪士尼度假区",
"jobOrder": 1,
"linkmanPhone": "021-20603105",
"linkman": "上海迪士尼度假区招聘中心",
"occupation": ["010000","010200","010207"]
}
],
"positionStrategy": "1",
"accountAbilities": "a\nc",
"jobNumber": "60302BR",
"title": "Host/Hostess, FL PT Test",
"cities": [
{
"type": "text",
"value": "Shanghai"
}
],
"department": "Parks, Experiences and Products",
"occupation": "Hotel",
"custom": "一线运营岗位",
"candidateSource": 0,
"employmentType": 1,
"requirement": "d",
"education": "",
"processId": 124,
"emailNotice": 1,
"source": 9,
"sourceId": 77,
"createDepartment": true,
"showDepartment": true,
"enableCustom": true,
"enableOccupation": true,
"createCustom": true
}
延伸问题:
"occupations":支持多个职能,猎聘用的这个字段
"occupation"的处理方式,
基础服务java端是这样处理的(最佳东方和实习僧用的是occupation,这两个渠道只支持一个职能。如果是”["开头,比如["01000","010200","010207"],只取最后一个职能。else,如果本来就一个职能就直接获取
一般前端(这里是对接python端)和后端参数约定好(接口文档),前端校验,后端校验的多,后端要操作数据库

数据库不支持数组,所以把occupations数组值放到字符串,java端要处理

附带之前提了一个bug
{
"pageNumber": 1,
"pageSize": 20,
"phaseId": 760,
"processId": 139,
"keyword": null,
"sex": 1,
"workYears": [2],
"cities": [],
"hasAttachment": "",
"intentionSalaryCode": [],
"channelId": [],
"positionStatus": "0",
"accountType": 1
}
返回数据:
{
"data": {
"code": "10001",
"message": "request param is wrong json format data or json field type is missmatch。detail error message:\u0028JSON parse error: Can not deserialize instance of java.lang.String out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token\n at [Source: java.io.PushbackInputStream@305efb4f; line: 1, column: 96] \u0028through reference chain: com.moseeker.vo.ats.request.ApplicationProfileSearchVO[\"workYears\"]\u0029\u0029"
},
"message": "success",
"status": 0
}

json.dumps 与 json.loads 实例
请参考 https://www.runoob.com/python3/python3-json.html
浙公网安备 33010602011771号