gpt4 book ai didi

python - 值错误 : Extra data: while loading JSON

转载 作者:太空宇宙 更新时间:2023-11-03 13:44:59 26 4
gpt4 key购买 nike

我在加载 JSON 时遇到问题。

response = conn.getresponse()
data = response.read().decode('utf-8')
print ("raw data >> ", data)
data1 = json.loads(data)
print (data1)

给我一​​个错误:

raw data >>  {"Len":"0000000000000376"}{"PipeType":2,"Content":{"ActionType":1,"Data":{"UserID":12,"RoomID":1,"UserData":{"NickName":"Koko","MoreAboutMe":null,"Age":21,"Man":false,"Area":9,"HaveCam":false,"isOldPoll":false,"LoginTime":635292689335460656,"RoomEnter":635292689335460656,"FacebookId":null,"Email":null,"FirstName":null,"LastName":null,"BirthDate":null,"FacebookLink":null,"Rank":-1}}}}{"Len":"0000000000000159"}{"PipeType":2,"Content":{"ActionType":3,"Data":{"Message":"no matter","ColorID":0,"UserID":13,"UserNick":"Jovani","SentDate":null,"Rank":null}}}
Exception in Tkinter callback
...
ValueError: Extra data: line 1 column 26 - line 1 column 587 (char 26 - 587)

有什么想法吗?

谢谢

最佳答案

json.loads不处理多个json数据。

以下是解决方法。

import json
import re

nonspace = re.compile(r'\S')
def iterparse(j):
decoder = json.JSONDecoder()
pos = 0
while True:
matched = nonspace.search(j, pos)
if not matched:
break
pos = matched.start()
decoded, pos = decoder.raw_decode(j, pos)
yield decoded

rawdata = '{"Len":"0000000000000376"}{"PipeType":2}'
for decoded in iterparse(rawdata):
print(decoded)

输出:

{u'Len': u'0000000000000376'}
{u'PipeType': 2}

关于python - 值错误 : Extra data: while loading JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22112439/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com