0

我收到错误 Traceback(最近一次调用最后一次):json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)

我的代码:```

HOST = '0.0.0.0'
PORT = 10001

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    conn, addr = s.accept()
    with conn:
        print('Connected by', addr)
        while True:
            dataByte = conn.recv(1024)
            if not dataByte: 
                print("Not data")
            myStr = dataByte.decode('utf-8').replace("'", '"')
        
            data = json.loads(myStr)
            s = json.dumps(data, indent=4, sort_keys=True)
            print(s)
```

原始数据字节 b'POST / HTTP/1.1\r\nHost: localhost:10001\r\nUser-Agent: Go-http-client/1.1\r\nContent-Length: 3153\r\nContent-Type: application/json\ r\nAccept-Encoding: gzip\r\n\r\n{"Event":"error","S":"0\ufffd\u000380\ufffd\u0002 \ufffd\u0003\u0002\u0001\u0002\u0002 \u0014\u0018DIv\ufffd\ufffd\xca\xb9\ufffd\ufffd\ufffd\ufffdSJa~\ufffd\ufffd\ufffd10\r\u0006\t*\ufffdH\ufffd\ufffd\r\u0001\u0001\u000b\u0005 \u00000O1\u000b0\t\u0006\u0003U\u0004\u0006\u0013\u0002BR1\u000e0\u000c\u0006\u0003U\u0004\u0008\u000c\u0005AAAAA1\r0\u000b\u0006\u0003U\u0004\u0007\u000c\u0004BBBB1 !0\u001f\u0006\u0003U\u0004\n\u000c\u0018Internet Widgits Pty Ltd0\u001e\u0017\r210524192942Z\u0017\r230827192942Z0!1\u000b0\t\u0006\u0003U\u0004\u0006\u0013\u0002GB1\u00120\ u0010\u0006\u0003U\u0004\u0003\u000c\tlocalhost0\ufffd\u0001\"0\r\u0006\t*\ufffdH\ufffd\ufffd\r\u0001\u0001\u0001\u0005\u0000\u0003\ufffd\u0001\u000f\u00000\ufffd\u0001\n\u0002\ufffd\u0001\u0001 \u0000\ufffdy\u0017\u0012|\ufffd\u0007\ufffd\u0006\ufffdwl\ufffd\ufffd\ufffdhu\ufffdAP\u000e\ufffd\ufffda\ufffd\u' b'0016\ufffdU\ufffdp\ufffd?\ufffd\ufffd\ufffdq2\u0000\ufffd\ufffd\ufffd9\ufffd\x7f\ufffd\ufffd\ufffd\ufffd%\ufffd\ufffd\ufffd\ufffd^B\ufffd\u0026* \ufffd\ufffd\ufffd\ufffd+\xd7\x98\ufffd\ufffdHw\ufffdl\ufffd\ufffd\ufffd\ufffd\xe3\x90\x95u\ufffdV\ufffd\xd6\x9d\ufffd\u0000d\ufffd\ufffd\xcd \x92\ufffd\u003c\ufffd\ufffd\ufffd\ufffdn%\ufffd-\u001e\ufffd\u00140ml\u0013A\ufffd#\ufffdr0$=\ufffd\ufffd\ufffd\ufffdr\ufffd\ufffd\ufffd\ufffd_| \ufffd\ufffd\ufffd\ufffdM\ufffd\t\ufffdK~\ufffdZ\ufffd\ufffd8\xd7\x9d\u0003\xc5\xb0\ufffdo\ufffd\ufffd\ufffd3\xca\x882\u003e\ufffd\x7fj\ ufffdc\ufffd\ufffdtuW\ufffdp\ufffd\ufffd\xcd\xaa.lc\ufffd\ufffd\ufffd\u00058\u001c\u001a,th3D4m\ufffd\u000b(\u0026\ufffdq\ufffd{\ufffd\ufffd\ufffd\ ufffd.\u003c\u000c\u003c\ufffd[)\xd2\x92\ufffd]\ufffd\ufffd\u0019t\u001e;E\ufffd%\ufffd\"\ufffd\ufffdM\ufffd/\u0001w\ufffdZ\u000c\ufffd\xc8\x85b\ufffdR\ufffdaT\ufffdVu\ufffd+\ufffd\ufffdU\ufffd;\u0019O\u0011\u0002\u0003\u0001\u0000\u0001\uffd :080\u000b\u0006\u0003U\u001d\u000f\u0004\u0004\u0003\u0002\u000400\u0013\u0006\u0003U\u001d%\u0004\u000c0\n\u0006\u0008+\u0006\u0008+\u0006 05\u0005\u0007\u0003\u00020\u0014\u0006\u0003U\u001d\u0011\u0004\r0\u000b\ufffd\tlocalhost0\r\u0006\t*\ufffdH\ufffd\ufffd\r\u0001\u0001\u000b \u0005\u0000\u0003\ufffd\u0001\u0001\u0000\ufffdV\ufffd.\ufffdT\ufffdl\ufffd\ufffd_\xd9\x93\u0015\ufffd\ufffdqJ\ufffd\ufffd\ufffd9\ufffdW\ufffd\u00105\ xdc\xa2\ufffd\ufffd:\ufffd\ufffdyBTJ\ufffd\ufffdF\ufffd\u0003\ufffd\ufffd\u001a#\ufffd\u0018\u0014S(C\ufffd\ufffd\ufffd\u000cWY\u0018\ufffd\u0026] \ufffd!6\ufffd\ufffd\ufffd}\ufffd\u0000Q\ufffd\ufffdF\u0001\ufffd\ufffd)\xd7\xb1\u001ar\ufffd\ufffdF\ufffd4y\u0007\ufffd\u0004\ufffd\u001b\ufffd@\ufffd \u0018\ufffdh\ufffdW\ufffd}\ufffd:\ufffdD\ufffd\ufffdAY\ufffd\ufffdK\ufffd\ufffdP\ufffd\ufffdB\u0008\ufffd\ufffdV+\ufffdo\ufffd\ufffd\ufffd\ufffd\ufffd\xd4 \x89itCP\ufffd?\ufffd\ufffd\ufffd\u0002\ufffdZ\u0010\ufffd04\x7fIR\ufffd\ufffd\ufffd\ufffd%;D\ufffd\ufffd\u001d\ufffdUrB\ufffd)z\ufffdZ\ufffdb\ufffd \ufffd\ufffd\ufffd\ufffd\xce\xba\ufffdE\xde\xa5\ufffd\u000f\ufffd\ufffdOx\ufffdi\ufffd%\x7f\u0014\ufffd\ufffd\ufffdZ\ufffd\ufffd\ufffdC\ufffduj\ ufffd\ufffd\ufffd\u001c\nmAt\u' b'fffd\ufffd\u001e\ufffd]\"XWX\ufffdv}\ufffd\ufffd\ufffdT9@HP\ufffd\ufffd\u001c\ufffd\ufffdF\u0004\ufffd $F\xce\x8b\ufffd\ufffd\ufffd\ufffd\ufffdI","Message":"发生错误:ERROR 505"}'

我的 str {"事件":"错误","S":"0\ufffd\u000380\ufffd\u0002\ufffd\u0003\u0002\u0001\u0002\u0002\u0014\u0018DIv\ufffd\ufffdʹ\ufffd\ufffd\ ufffd\ufffdSJa~\ufffd\ufffd\ufffd10\r\u0006\t*\ufffdH\ufffd\ufffd\r\u0001\u0001\u000b\u0005\u00000O1\u000b0\t\u0006\u0003U\u0004\u0006\u0013\ u0002BR1\u000e0\u000c\u0006\u0003U\u0004\u0008\u000c\u0005AAAAA1\r0\u000b\u0006\u0003U\u0004\u0007\u000c\u0004BBBB1!0\u001f\u0006\u0003U\u0004\n\u000c\u0018I\ ufffdI","Message":"发生错误:ERROR 505"}

4

1 回答 1

0

您的字符串似乎包含损坏的 Unicode 转义。转义\u后面是编码代码点的四个\u000380字符,例如,被解析为\u0003(aka ascii Control-C) 后跟普通数字"80"

无论如何,当我尝试重现您的问题时,我得到的错误与您不同:

json.decoder.JSONDecodeError:无效的控制字符:第 1 行第 25 列(字符 24)

由于不知道还会发生什么,请创建一个包含您尝试解析的响应数据的测试字符串并将其嵌入到您的 Python 代码中(编辑您的问题,不要尝试在评论中这样做)。

我们只需要包含数据字符串和json.loads()调用...以及错误消息的代码。然后我们也许能够看到你所看到的,并取得进展。

于 2021-07-30T14:17:44.647 回答