{"data":[{"node":"emqx@127.0.0.1","topic":"4e4a3668bf514c6f89738c5f3eec6285"},{"node":"emqx@127.0.0.1","topic":"05f170438ab44d769f9f67c1406316c2"},{"node":"emqx@127.0.0.1","topic":"fcef086b0ea443429cc867a35fb630fd"},{"node":"emqx@127.0.0.1","topic":"topic_aaa"}],"meta":{"count":4,"hasnext":false,"limit":20,"page":1}}为字符串,取出topic的值
时间: 2023-07-05 22:36:33 浏览: 57
可以使用 JSON 解析库将该字符串解析为 JSON 对象,然后遍历其中的数据,取出每个对象中的 "topic" 字段的值即可。以下是 Python 代码示例:
```python
import json
json_str = '{"data":[{"node":"emqx@127.0.0.1","topic":"4e4a3668bf514c6f89738c5f3eec6285"},{"node":"emqx@127.0.0.1","topic":"05f170438ab44d769f9f67c1406316c2"},{"node":"emqx@127.0.0.1","topic":"fcef086b0ea443429cc867a35fb630fd"},{"node":"emqx@127.0.0.1","topic":"topic_aaa"}],"meta":{"count":4,"hasnext":false,"limit":20,"page":1}}'
json_obj = json.loads(json_str)
for item in json_obj['data']:
print(item['topic'])
```
输出结果为:
```
4e4a3668bf514c6f89738c5f3eec6285
05f170438ab44d769f9f67c1406316c2
fcef086b0ea443429cc867a35fb630fd
topic_aaa
```
相关问题
{"data":[{"node":"emqx@127.0.0.1","topic":"4e4a3668bf514c6f89738c5f3eec6285"},{"node":"emqx@127.0.0.1","topic":"05f170438ab44d769f9f67c1406316c2"},{"node":"emqx@127.0.0.1","topic":"fcef086b0ea443429cc867a35fb630fd"},{"node":"emqx@127.0.0.1","topic":"topic_aaa"}],"meta":{"count":4,"hasnext":false,"limit":20,"page":1}}取出里面topic的值
可以使用以下代码来解析JSON数据并获取topic的值:
```python
import json
json_data = '{"data":[{"node":"emqx@127.0.0.1","topic":"4e4a3668bf514c6f89738c5f3eec6285"},{"node":"emqx@127.0.0.1","topic":"05f170438ab44d769f9f67c1406316c2"},{"node":"emqx@127.0.0.1","topic":"fcef086b0ea443429cc867a35fb630fd"},{"node":"emqx@127.0.0.1","topic":"topic_aaa"}],"meta":{"count":4,"hasnext":false,"limit":20,"page":1}}'
data = json.loads(json_data)
topics = [d['topic'] for d in data['data']]
print(topics)
```
输出结果为:
```
['4e4a3668bf514c6f89738c5f3eec6285', '05f170438ab44d769f9f67c1406316c2', 'fcef086b0ea443429cc867a35fb630fd', 'topic_aaa']
```
阅读全文