如何解决Json错误:TypeError:需要类似byteslike的对象,而不是“str”

2024-10-02 02:45:03 发布

您现在位置:Python中文网/ 问答频道 /正文

请使用正确的语法进行更正导致错误的pythoh代码段:

def reliable_send(self, data):
        json_data = json.dumps(data)
        self.connection.send(json_data)

错误为:

self.connection.send(json_数据)在json_数据中

TypeError:需要类似字节的对象,而不是“str”


Tags: 数据selfsendjsondata字节def代码段
1条回答
网友
1楼 · 发布于 2024-10-02 02:45:03

您需要在发送之前对数据进行编码,这应该是解决方案

def reliable_send(self, data):
    json_data = json.dumps(data)
    self.connection.send(json_data.encode())

相关问题 更多 >

    热门问题