简体   繁体   English

如何解决 Ubuntu 中的 Json 解码错误

[英]How to resolve Json decode error in Ubuntu

I am running python script on Window 10. In the python script, I am using json library.我在 Window 10 上运行 python 脚本。在 python 脚本中,我使用的是 json 库。 When I run the same script on Ubuntu 20.04(running on VMware), I do see json decode error happening.当我在 Ubuntu 20.04(在 VMware 上运行)上运行相同的脚本时,我确实看到发生了 json 解码错误。 This behaviour I dont see when I run in Windows 10.我在 Windows 10 中运行时看不到这种行为。

The following is the error I do get when I run the script in Ubuntu以下是我在 Ubuntu 中运行脚本时得到的错误

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "uiControl.py", line 83, in getTcpData
    self.taskObj = json.loads(data.decode('utf-8'))
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 5 column 2 (char 73)

In function on_message, I am printing the data received.在函数 on_message 中,我正在打印接收到的数据。 The following is the data I receive : b'{"code":"101","user":"ssmr","evNumber":"TS15EC1100"}'以下是我收到的数据: b'{"code":"101","user":"ssmr","evNumber":"TS15EC1100"}'

I call the function addToTaskQueue() to store the received data and then try to parse the data using function BackendParser()我调用函数 addToTaskQueue() 来存储接收到的数据,然后尝试使用函数 BackendParser() 解析数据

def on_message(self,client, userdata, msg):
    print(msg.payload)
    self.taskObj = json.loads(msg.payload )
    self.taskObj["commType"]= "mqtt"
    self.taskObj["transactionType"]= "rx"        
    taskScheduler.addToTaskQueue(self.taskObj)

def BackendParser(msg):
    if(msg["code"] == "101"):
        Backend.userName = msg["user"]
        Backend.evNumber = msg["evNumber"]
        Backend.evChargeControl = "On"
        if(Backend.requestStatus == ""):
            Backend.requestStatus = "new"


class taskScheduler():
    global qTaskList

    qTaskList = queue.Queue()     
    def __init__(self):
        super().__init__()
        self.tcpCon = tcpServerClient("client")
        self.mqttCon = mqttComm()
        print("Initiated Task Scheduler class")

    @staticmethod
    def addToTaskQueue(item):
        if not qTaskList.full():
            #print("Task added")
            qTaskList.put(item)

    def executeFromTaskQueue(self):
        if not qTaskList.empty():
            item = qTaskList.get()
            if("mqtt" == item["commType"]):
                if("tx" == item["transactionType"]):
                    pubTopic = item["topic"]
                    del item["commType"]
                    del item["transactionType"]
                    del item["topic"]
                    self.mqttCon.mqttSend(item,pubTopic)
                elif("rx" == item["transactionType"]):
                    BackendParser(item)
            elif("tcp" == item["commType"]):
                if("tx" == item["transactionType"]):
                    del item["commType"]
                    del item["transactionType"]
                    tcpServerClient.sendTcpData(item)
                elif("rx" == item["transactionType"]):
                    BackendParser(item)

I figured out the error I was using the following function getTcpData to receive the data.我发现了我使用以下函数 getTcpData 接收数据时出现的错误。 I tried printing the data as received and noticed that there were \\n characters in the message received.This was not issue when the script was executed in Windows 10. I now added the routine to remove the \\n character and now it works fine in Ubuntu.我尝试打印收到的数据,并注意到收到的消息中有 \\n 个字符。在 Windows 10 中执行脚本时这不是问题。我现在添加了删除 \\n 字符的例程,现在它可以正常工作乌班图。

def getTcpData(self):  
    print("Waiting for tcp data")
    while True:
        if(tcpServerClient.clientsocket != None):
            data=tcpServerClient.clientsocket.recv(1024)
            if data:
                print(data)
                self.taskObj = json.loads(data.decode('utf-8'))
                self.taskObj["commType"]= "tcp"
                self.taskObj["transactionType"]= "rx"
                taskScheduler.addToTaskQueue(self.taskObj)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM