b'{"BusinessEntityID": 23, "Title": null, "FirstName": "Mary", "MiddleName": "E", "LastName": "Gibson", "Suffix": null, "JobTitle": "Marketing Specialist", "PhoneNumber": "531-555-0183", "PhoneNumberType": "Work", "EmailAddress": "mary0@adventure-works.com", "EmailPromotion": 0, "AddressLine1": "3928 San Francisco", "AddressLine2": null, "City": "Everett", "StateProvinceName": "Washington", "PostalCode": "98201", "CountryRegionName": "United States", "AdditionalContactInfo": null}'
我正在用kafka使用上面的内容,我想将其提取到python dict中。我尝试过解码和执行json.loads,但是触发了这个错误 Expecting value: line 1 column 1 (char 0)
这也不起作用。这是我的密码:
try:
while True:
msg = consumer.poll(1.0)
if msg is None:
print('...')
elif msg.error() is None:
msg_string = ''.join(map(chr, msg.value()))
print(type(msg_string)) # <class 'str'>
record = json.loads(msg_string) # ERROR: Expecting value: line 1 column 1 (char 0
print(record)
elif msg.error() is not None:
print(f'Msg Error {msg.error()}')
except Exception as e:
print(f'ERROR: {e}')
谢谢。
1条答案
按热度按时间eivnm1vs1#
首先,使用
a.decode('utf-8')
然后使用json.loads
把它转换成JSON
对象谢谢您