【发布时间】:2013-02-21 19:14:35
【问题描述】:
我正在尝试在 Python 中解析一些数据我有一些 JSON:
{
"data sources": [
"http://www.gcmap.com/"
],
"metros": [
{
"code": "SCL",
"continent": "South America",
"coordinates": {
"S": 33,
"W": 71
},
"country": "CL",
"name": "Santiago",
"population": 6000000,
"region": 1,
"timezone": -4
},
{
"code": "LIM",
"continent": "South America",
"coordinates": {
"S": 12,
"W": 77
},
"country": "PE",
"name": "Lima",
"population": 9050000,
"region": 1,
"timezone": -5
}
]
}
如果我想将 "metros" 数组解析为 Python 类 Metro 对象的数组,我将如何设置该类?
我在想:
class Metro(object):
def __init__(self):
self.code = 0
self.name = ""
self.country = ""
self.continent = ""
self.timezone = ""
self.coordinates = []
self.population = 0
self.region = ""
所以我想遍历每个 Metro 并将数据放入相应的 Metro 对象中,并将该对象放入 Python 对象数组中...如何循环遍历 JSON Metro?
【问题讨论】:
-
我不明白这个问题。当你有 JSON 时,你有一个对象,你可以从这个对象中获取 Metros 列表