简体   繁体   中英

Can't print a json value

i have this code in python whoose purpose is to send a text file .c .txt (whatever, i've been sending a helloworld.c) through a websocket.

The problem is when i test it, the code doesn't go beyond print("I'm here!")

 def onMessage_function(self, client_id, message): 
        print("Here's the message I received " + message + "\n\n\n")
        dumpedMSG = json.dumps(message)
        loadedMSG = json.loads(dumpedMSG)
        if 'file_name' in loadedMSG:
            print("I'm here!")
            print(loadedMSG['file_name'])
        else:
        # do something else here.

Thank you!

It's hard to tell, but does this work?

def onMessage_function(self, client_id, message): 
    print("Here's the message I received " + message + "\n\n\n")
    loadedMSG = json.loads(message)
    if 'file_name' in loadedMSG:
        print("I'm here!")
        print(loadedMSG['file_name'])
    else:
    # do something else here.

In the original loadedMSG would be the same as message , so 'file_name' in loadedMSG would be a substring check rather than a check for a dictionary key. The print after "I'm here!" would then throw an exception, which you might not see if you're only receiving what is sent over the socket.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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