Python 3.x 在python3中操作类型byte

Python 3.x 在python3中操作类型byte,python-3.x,Python 3.x,你能帮我从这个字节中获取报告的id吗 b'{ "message":"The report with id 9520 was created but it is invalid!", "violations":"{\\"schemaLocation\\":\\"#\\",\\"pointerToViolation\\":\\"#\\",\\"causingExceptions\\":[{\\"schemaLocation\\":\\"#\\",\\"pointerToViolation\\":\

你能帮我从这个字节中获取报告的id吗

b'{
"message":"The report with id 9520 was created but it is invalid!", 
"violations":"{\\"schemaLocation\\":\\"#\\",\\"pointerToViolation\\":\\"#\\",\\"causingExceptions\\":[{\\"schemaLocation\\":\\"#\\",\\"pointerToViolation\\":\\"#\\",\\"causingExceptions\\":[],\\"keyword\\":\\"required\\",\\"message\\":\\"required key [reportType] not found\\"},{\\"schemaLocation\\":\\"#\\",\\"pointerToViolation\\":\\"#\\",\\"causingExceptions\\":[],\\"keyword\\":\\"required\\",\\"message\\":\\"required key [finishedAt] not found\\"},{\\"schemaLocation\\":\\"#/properties/hosts/items\\",\\"pointerToViolation\\":\\"#/hosts/0\\",\\"causingExceptions\\":[],\\"keyword\\":\\"required\\",\\"message\\":\\"required key [applications] not found\\"}],\\"message\\":\\"3 schema violations found\\"}",    
"valid":false
}'

您没有清楚地提到所有可能的输入。但是,对于给定的输入,下面的程序将起作用!这有点硬编码

import json

data=b'{"message":"The report with id 9520 was created but it is invalid!", "violations":"{\\"schemaLocation\\":\\"#\\",\\"pointerToViolation\\":\\"#\\",\\"causingExceptions\\":[{\\"schemaLocation\\":\\"#\\",\\"pointerToViolation\\":\\"#\\",\\"causingExceptions\\":[],\\"keyword\\":\\"required\\",\\"message\\":\\"required key [reportType] not found\\"},{\\"schemaLocation\\":\\"#\\",\\"pointerToViolation\\":\\"#\\",\\"causingExceptions\\":[],\\"keyword\\":\\"required\\",\\"message\\":\\"required key [finishedAt] not found\\"},{\\"schemaLocation\\":\\"#/properties/hosts/items\\",\\"pointerToViolation\\":\\"#/hosts/0\\",\\"causingExceptions\\":[],\\"keyword\\":\\"required\\",\\"message\\":\\"required key [applications] not found\\"}],\\"message\\":\\"3 schema violations found\\"}","valid":false}'


data = json.loads(data)["message"].split()
数据
是包含
消息
键值的列表!您可以对其进行索引以获得所需的值

>>> data
['The', 'report', 'with', 'id', '9520', 'was', 'created', 'but', 'it', 'is', 'invalid!']

这个输入的格式是什么?是json还是字符串?这是一个request.post的response.content,因此您的resonse.content中的这个data=resp.json()的jsonGive输出解决了这个问题,谢谢