JSON格式的响应结果,

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'])

结果:

多云

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值