python - 如何获取json数据并将其转换为字符串
问题描述
我是 python 中 websockets 和 json 的新手。我只想知道如何将 json 数据转换为字符串,就像这样
{'data': {'id': 26, 'level': 2, 'message': 'Something was Detected'}}
进入
"26"
"2"
"Something was Detected"
或任何格式只是为了使其成为一个字符串。
我正在从本地 websocket 接收我的 json 数据,这是代码。
async with websockets.connect(url) as websocket:
data = dict(topic='device:10', event="phx_join", payload={}, ref=0)
await websocket.send(json.dumps(data))
# Json data will be receive here
message = await websocket.recv()
# then it will be printed here
print(message)
# I need to convert the message into string
asyncio.get_event_loop().run_until_complete(hello('ws://localhost:4000/socket/websocket'))
解决方案
像这样的东西:
data = json.loads(message)
list_of_strings = [str(v) for k, v in data['data'].items()]
推荐阅读
- angular - 带有来自 API 的动态数据的 Angular highchart
- javascript - jQuery无法获取按钮旁边的textarea
- python - 在时间序列中编辑密集的 x 标签
- ruby - 如何存根返回集合 rspec 的方法
- c# - Selenium 截图到内存
- c - 如何将多个数组组合为一个字符串
- python-3.x - 使用 Python 和 AWS 解析医学测试并提取表和键值?
- r - 根据第二个小标题中的值过滤小标题
- javascript - 我该怎么做安全拆分()
- git - 从分支 x 创建一个分支,将分支 x 合并到其父分支并删除它。创建的分支会发生什么?