{'success': True, 'message': 'OK', 'data': [{'id': 977132, 'name': 'innerserver-gray', 'alias': 'innerserver-gray', 'path': 'infoflow-monitor_infoflow_innerserver-gray', 'parentId': 970023, 'sourceType': None, 'type': 'app', 'operationType': 2}, {'id': 977150, 'name': 'auth', 'alias': 'auth', 'path': 'infoflow-monitor_work_auth', 'parentId': 970026, 'sourceType': None, 'type': 'app', 'operationType': 2} 这个已经是python转换为json格式如何去除path后面的值
时间: 2023-06-02 12:05:14 浏览: 69
m-message:vue的消息插件
可以使用Python中的字符串处理方法来去除path后面的值,具体的方法如下所示:
假设你已经将上述JSON格式的字符串保存到了变量data中,可以使用如下的代码来去除path后面的值:
``` python
import json
data = '{"success": true, "message": "OK", "data": [{"id": 977132, "name": "innerserver-gray", "alias": "innerserver-gray", "path": "infoflow-monitor_infoflow_innerserver-gray", "parentId": 970023, "sourceType": null, "type": "app", "operationType": 2}, {"id": 977150, "name": "auth", "alias": "auth", "path": "infoflow-monitor_work_auth", "parentId": 970026, "sourceType": null, "type": "app", "operationType": 2}]}'
json_dict = json.loads(data)
for item in json_dict['data']:
if 'path' in item:
item['path'] = item['path'].split('_')[0]
print(json.dumps(json_dict))
```
运行上述代码后,输出的结果将会是这样的:
``` json
{"success": true, "message": "OK", "data": [{"id": 977132, "name": "innerserver-gray", "alias": "innerserver-gray", "path": "infoflow-monitor", "parentId": 970023, "sourceType": null, "type": "app", "operationType": 2}, {"id": 977150, "name": "auth", "alias": "auth", "path": "infoflow-monitor", "parentId": 970026, "sourceType": null, "type": "app", "operationType": 2}]}
```
如上所示,我们成功的去除了path后面的值,只保留了path前面的部分。
阅读全文