我正在尝试在 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
对象中,然后将该对象放入 Python 对象数组中......如何循环遍历 JSON 地铁?