2013-10-04 32 views
1

編輯:更新的代碼,不再GIVES指定的錯誤字典以JSON在Python

我試圖編碼以下爲JSON格式:

class Map: 
    """ 
    Storage for cities and routes 
    """ 

    def __init__(self): 
     self.cities = {} 
     self.routes = {} 

類路線: 「」」 百貨路線信息 「」 「

def __init__(self, src, dest, dist): 
    self.flight_path = src + '-' + dest 
    self.src = src 
    self.dest = dest 
    self.dist = dist 

def to_json(self): 
    return {'source': self.src, 'destination': self.dest, 
    'distance': self.dist} 

類城市: 」「」 小號tores城市信息 「」」

def __init__(self, code, name, country, continent, 
      timezone, coordinates, population, region): 
    self.code = code 
    self.name = name 
    self.country = country 
    self.continent = continent 
    self.timezone = timezone 
    self.coordinates = coordinates 
    self.population = population 
    self.region = region 

def to_json(self): 
    return {'code': self.code, 'name': self.name, 'country': self.country, 
    'continent': self.continent, 'timezone': self.timezone, 
    'coordinates': self.coordinates, 'population': self.population, 
    'region': self.region} 

我想‘編碼所有的路由信息​​的路由‘部分存儲在map.routes城市’編碼全部存儲在map.cities城市的信息蟒蛇的部分和’

我嘗試:

def map_to_json(my_file, air_map): 
    """ 
    Saves JSON Data 
    """ 
    with open(my_file, 'w') as outfile: 
     for entry in air_map.cities: 
      json.dumps(air_map.cities[entry].to_json(), outfile) 
     for entry in air_map.routes: 
      json.dumps(air_map.routes[entry].to_json(), outfile) 

    outfile.close() 

其中air_map是地圖和my_file是文件路徑。

我得到以下錯誤:

> Jmap.map_to_json('Resources/map_save.json', air_map) File 
> "JSONToMap.py", line 53, in map_to_json 
>  json.dumps(air_map.cities, outfile) File "C:\Python27\lib\json\__init__.py", line 250, in dumps 
>  sort_keys=sort_keys, **kw).encode(obj) File "C:\Python27\lib\json\encoder.py", line 207, in encode 
>  chunks = self.iterencode(o, _one_shot=True) File "C:\Python27\lib\json\encoder.py", line 270, in iterencode 
>  return _iterencode(o, 0) File "C:\Python27\lib\json\encoder.py", line 184, in default 
>  raise TypeError(repr(o) + " is not JSON serializable") TypeError: <City.City instance at 0x0417FC88> is not JSON serializable 
> >>> 

我很新的蟒蛇,和JSON,因此幫助將不勝感激。

謝謝

+1

列表只需將您的類不是JSON序列化。 檢查鏈接: http://stackoverflow.com/questions/3768895/python-how-to-make-a-class-json-serializable – Rami

回答

2

你不能直接序列化對象。試試這個:

class City: 
    """ 
    Stores city info 
    """ 

    def __init__(self, code, name, country, continent, 
       timezone, coordinates, population, region): 
     self.code = code 
     self.name = name 
     self.country = country 
     self.continent = continent 
     self.timezone = timezone 
     self.coordinates = coordinates 
     self.population = population 
     self.region = region 

    def to_json(self): 
     return {'code': self.code, 'name': self.name, 
       'country': self.country, 
       #rest of the attributes... 
       } 

,並同時呼籲,

json.dumps(air_map.cities.to_json(), outfile) 

,類似的還有Routes模型了。

如果你正在處理對象的列表,你總是可以做:

json.dumps([city.to_json() for city in air_map.cities], outfile) 

如果air_map.cities是城市

+0

他可能會逃脫只是'def to_json(self):返回自我.__ dict__'。 –

+0

@TimPeters感謝您指出這一點。如果__all__字段在序列化對象中是必需的,這絕對是實現它的好方法。 – karthikr

+0

我我的代碼更新爲: 開放(my_file, 'W')爲OUTFILE: 在air_map.cities條目: json.dumps(air_map.cities [進入] .to_json(),OUTFILE) 入門在air_map.routes中: json.dumps(air_map.routes [entry] .to_json(),outfile) outfile.close() **** outfile.close()是否工作? **** –