使用Python从JSON获取值

编程入门 行业动态 更新时间:2024-10-25 04:24:59
本文介绍了使用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"]

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

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

我需要怎么做才能获得lat和lon的值? (444和555)

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获取值

本文发布于:2023-10-29 08:22:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539197.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Python   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!