I have a simple Websockets server in python, it receives messages from Android app clients, I tried to make the message payload from the client in JSON but I felt. It is only wo
You should use the json
Python package. To have a JSON, you could simply do import json
and json.dumps(message)
.
I tried this way and it worked for me, I converted the message into a dictionary using ast
and then used the new message as I wanted:
formattedMessage = ast.literal_eval(format(message))
self.write_message(formattedMessage["name"])
Will something like this work for you?
import json
# assume this is the JSON you receive
text = json.dumps(dict(name='Jack', age='24'))
# show the text to be converted
print(text)
# outputs: {"name": "Jack", "age": "24"}
# load a string and convert to Python object
# see `json` module more for details
obj = json.loads(text)
Use a Json package in python
import json
data = json.loads(your_var)
In data variable you get a json format data
hope this will help you