访问 simplejson.load() 输出中的元素
我有一个从 simplejson.load() 函数返回的数据的字典(?)。它看起来像这样...
{'status': 'OK', 'results': [{'geometry': {'location_type': 'APPROXIMATE', 'bounds': {'northeast': {'lat': 53.86121, 'lng': -2.045072}, 'southwest': {'lat': 53.80570600000001, 'lng': -2.162588}}, 'viewport': {'northeast': {'lat': 53.8697753, 'lng': -2.0725853}, 'southwest': {'lat': 53.81711019999999, 'lng': -2.2006447}}, 'location': {'lat': 53.84345099999999, 'lng': -2.136615}}, 'address_components': [{'long_name': 'Trawden', 'types': ['sublocality', 'political'], 'short_name': 'Trawden'}, {'long_name': 'Colne', 'types': ['locality', 'political'], 'short_name': 'Colne'}, {'long_name': 'Lancashire', 'types': ['administrative_area_level_2', 'political'], 'short_name': 'Lancs'}, {'long_name': 'United Kingdom', 'types': ['country', 'political'], 'short_name': 'GB'}], 'formatted_address': 'Trawden, Colne, Lancashire, UK', 'types': ['sublocality', 'political']}]}
我如何获得例如结果->几何->位置->纬度?
这个结构是一个普通的Python字典吗?
编辑:请有人也解释 simplejson.dumps() 函数。我觉得这些文档不太有启发性。
谢谢
非OP编辑:这是JSON,打印得很漂亮:
{
"status":"OK",
"results":[
{
"geometry":{
"location":{
"lat":53.843450999999988,
"lng":-2.1366149999999999
},
"location_type":"APPROXIMATE",
"viewport":{
"northeast":{
"lat":53.869775300000001,
"lng":-2.0725853000000001
},
"southwest":{
"lat":53.817110199999988,
"lng":-2.2006446999999998
}
},
"bounds":{
"northeast":{
"lat":53.86121,
"lng":-2.0450719999999998
},
"southwest":{
"lat":53.805706000000008,
"lng":-2.162588
}
}
},
"address_components":[
{
"long_name":"Trawden",
"short_name":"Trawden",
"types":[
"sublocality",
"political"
]
},
{
"long_name":"Colne",
"short_name":"Colne",
"types":[
"locality",
"political"
]
},
{
"long_name":"Lancashire",
"short_name":"Lancs",
"types":[
"administrative_area_level_2",
"political"
]
},
{
"long_name":"United Kingdom",
"short_name":"GB",
"types":[
"country",
"political"
]
}
],
"formatted_address":"Trawden, Colne, Lancashire, UK",
"types":[
"sublocality",
"political"
]
}
]
}
i have a dictionary(?) of data returned from the simplejson.load() function. it looks like this...
{'status': 'OK', 'results': [{'geometry': {'location_type': 'APPROXIMATE', 'bounds': {'northeast': {'lat': 53.86121, 'lng': -2.045072}, 'southwest': {'lat': 53.80570600000001, 'lng': -2.162588}}, 'viewport': {'northeast': {'lat': 53.8697753, 'lng': -2.0725853}, 'southwest': {'lat': 53.81711019999999, 'lng': -2.2006447}}, 'location': {'lat': 53.84345099999999, 'lng': -2.136615}}, 'address_components': [{'long_name': 'Trawden', 'types': ['sublocality', 'political'], 'short_name': 'Trawden'}, {'long_name': 'Colne', 'types': ['locality', 'political'], 'short_name': 'Colne'}, {'long_name': 'Lancashire', 'types': ['administrative_area_level_2', 'political'], 'short_name': 'Lancs'}, {'long_name': 'United Kingdom', 'types': ['country', 'political'], 'short_name': 'GB'}], 'formatted_address': 'Trawden, Colne, Lancashire, UK', 'types': ['sublocality', 'political']}]}
How do I get at e.g. results->geometry->location->lat ?
Is this structure a regular python dictionary?
EDIT: please could someone also explain the simplejson.dumps() function. I don't find the docs very enlightening.
thanks
Edit by non-OP: here's the JSON, pretty-printed:
{
"status":"OK",
"results":[
{
"geometry":{
"location":{
"lat":53.843450999999988,
"lng":-2.1366149999999999
},
"location_type":"APPROXIMATE",
"viewport":{
"northeast":{
"lat":53.869775300000001,
"lng":-2.0725853000000001
},
"southwest":{
"lat":53.817110199999988,
"lng":-2.2006446999999998
}
},
"bounds":{
"northeast":{
"lat":53.86121,
"lng":-2.0450719999999998
},
"southwest":{
"lat":53.805706000000008,
"lng":-2.162588
}
}
},
"address_components":[
{
"long_name":"Trawden",
"short_name":"Trawden",
"types":[
"sublocality",
"political"
]
},
{
"long_name":"Colne",
"short_name":"Colne",
"types":[
"locality",
"political"
]
},
{
"long_name":"Lancashire",
"short_name":"Lancs",
"types":[
"administrative_area_level_2",
"political"
]
},
{
"long_name":"United Kingdom",
"short_name":"GB",
"types":[
"country",
"political"
]
}
],
"formatted_address":"Trawden, Colne, Lancashire, UK",
"types":[
"sublocality",
"political"
]
}
]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。如果将其存储在名为
d
的变量中,那么您将使用...等等。请注意其中的
[0]
,因为带有键'geometry'
的字典位于列表内。simplejson.load()
将 JSON 对象映射到 Pythondict
,将 JSON 列表映射到list
。非常简单;别想太多。simplejson.dumps()
只是执行与simplejson.loads()
相反的操作 - 它接受任何标准 Python 对象,并将其转储到一个字符串,该字符串是该对象的 JSON 表示形式目的。例如:Yes, it is. If you store it in a variable named
d
, then you would use...et cetera. Notice the
[0]
there due to the fact that the dict with key'geometry'
is inside a list.simplejson.load()
maps JSON objects to Pythondict
s and JSON lists tolist
s. Very straightforward; don't overthink it.simplejson.dumps()
simply does the opposite ofsimplejson.loads()
- it takes any standard Python object, and dumps it to a string which is a JSON representation of that object. For instance: