res = {'reason': '查询成功',
'result': [{'wid': '00', 'weather': '晴'},
{'wid': '01', 'weather': '多云'},
{'wid': '02', 'weather': '阴'},
{'wid': '03', 'weather': '阵雨'},
{'wid': '04', 'weather': '雷阵雨'},
{'wid': '05', 'weather': '雷阵雨伴有冰雹'}],
'error_code': 0}
1、获取响应结果的键和值
key=res.keys()
value=res.values()
结果:
dict_keys(['reason', 'result', 'error_code']) dict_values(['查询成功', [{'wid': '00', 'weather': '晴'}, {'wid': '01', 'weather': '多云'}, {'wid': '02', 'weather': '阴'}, {'wid': '03', 'weather': '阵雨'}, {'wid': '04', 'weather': '雷阵雨'}, {'wid': '05', 'weather': '雷阵雨伴有冰雹'}], 0])
2、获取result 中每个字典中的key和value值
list=res['result']
for i in list:
for key ,value in i.items():
print(key ,value)
结果:
wid 00
weather 晴
wid 01
weather 多云
wid 02
weather 阴
wid 03
weather 阵雨
wid 04
weather 雷阵雨
wid 05
weather 雷阵雨伴有冰雹
3、取出wid“03”的天气
{'wid': '03', 'weather': '阵雨'}
list[3]['weather']
结果:
阵雨
4、输出数组中指定的值:
res={'reason': '查询成功',
'result': [{'wid': '00', 'weather': '晴'},
{'wid': '01', 'weather': '多云'},
{'wid': '02', 'weather': '阴'},
{'wid': '03', 'weather': '阵雨'},
{'wid': '04', 'weather': '雷阵雨'},
{'wid': '05', 'weather': '雷阵雨伴有冰雹'}],
'error_code': 0}
查询编号01的天气是什么?
list=res['result']
for i in list:
if ("01" in i.values()):
print(list[list.index(i)]['weather'])
结果:
多云
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧