使用Python从JSON获取特定字段值
我正在收集我们网络上被关闭的设备 我正在跑步:使用Python从JSON获取特定字段值,python,json,Python,Json,我正在收集我们网络上被关闭的设备 我正在跑步: rjson = r.json() print(json.dumps(rjson, indent = 5)) 它返回: { "paging": { "size": 13 }, "data": { "devices": [ { "hostName": "host_1", "
rjson = r.json()
print(json.dumps(rjson, indent = 5))
它返回:
{
"paging": {
"size": 13
},
"data": {
"devices": [
{
"hostName": "host_1",
"networkAddress": "111.111.111.111",
"bestState": "Down",
"worstState": "Down",
},
{
"hostName": "host_2",
"networkAddress": "111.111.111.111",
"bestState": "Down",
"worstState": "Down",
},
{
"hostName": "host_3",
"networkAddress": "111.111.111.111",
"bestState": "Down",
"worstState": "Down",
},
我想运行这个json并创建一个包含返回的每个主机名的列表。我该怎么做?您可以尝试:
from operator import itemgetter
host_names = list(map(itemgetter('hostName'), rjson['data']['devices']))
print(host_names)
输出:
['host_1', 'host_2', 'host_3']
您尝试过什么吗?作为一个新用户,从开始阅读。关于您的任务,解码JSON并从数据中提取相关信息。只需在谷歌上快速搜索您的答案: