How to fetch values from dict

83 Views Asked by At

I am implementing a chat client gui whith server-push functionality. The messages that will be sent will look like this:

yourMessage = {'name': 'David', 'text': 'hello world'}

I recieve a server-push by a call to the streamHandler

def streamHandler(incomingData):
    if incomingData["event"] == "put":
        if incomingData["path"] == "/":
            if incomingData["data"] != None:
                for key in incomingData["data"]:
                    message = incomingData["data"][key]
                    handleMessage(message)
        else:
            message = incomingData["data"]
            handleMessage(message)

Then I have the function handleMessage that should retrieve the values of name and text:

def handleMessage(message):
    for key in message.values():
        printToMessages(key)

But now I get this Error: 'str' object has no attribute 'values' I have tried making message to dict but with no success, any ideas?

1

There are 1 best solutions below

2
quamrana On

Perhaps the message parameter is a json string.

If so you can have:

import json

def handleMessage(text):
    message = json.loads(text)
    for key in message.values():
        printToMessages(key)