使用Python从JSON获取值 [英] Getting values from JSON using Python

查看:653
本文介绍了使用Python从JSON获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从JSON字符串中检索值时,它给了我一个错误:

While I am trying to retrieve values from JSON string, it gives me an error:

data = json.loads('{"lat":444, "lon":555}')
return data["lat"]

但是,如果我遍历数据,它将为我提供元素(latlon),但没有值:

But, if I iterate over the data, it gives me the elements (lat and lon), but not the values:

data = json.loads('{"lat":444, "lon":555}')
    ret = ''
    for j in data:
        ret = ret + ' ' + j
return ret

哪个返回:lat lon

我需要怎么做才能获得latlon的值? (444555)

What do I need to do to get the values of lat and lon? (444 and 555)

推荐答案

如果要遍历字典的键和值,请执行以下操作:

If you want to iterate over both keys and values of the dictionary, do this:

for key, value in data.items():
    print key, value

这篇关于使用Python从JSON获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆