python中JSON的字典
编辑:更新的代码,不再给出指定的错误 我正在尝试将以下内容编码为json格式:python中JSON的字典,python,python-2.7,encoding,json,Python,Python 2.7,Encoding,Json,编辑:更新的代码,不再给出指定的错误 我正在尝试将以下内容编码为json格式: class Map: """ Storage for cities and routes """ def __init__(self): self.cities = {} self.routes = {} 班级路线: """ 存储路线信息 “”“ 级别城市: """ 存储城市信息 “”“ 我想要python的“cities”部分编码map.citie
class Map:
"""
Storage for cities and routes
"""
def __init__(self):
self.cities = {}
self.routes = {}
班级路线:
"""
存储路线信息
“”“
级别城市:
"""
存储城市信息
“”“
我想要python的“cities”部分编码map.cities中存储的所有城市信息,以及“routes”部分编码map.routes中存储的所有路线信息
我的尝试:
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是一个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
> >>>
>Jmap.map_to_json('Resources/map_save.json',air_map)文件
>“JSONToMap.py”,map_to_json中的第53行
>json.dumps(air\u map.cities,outfile)文件“C:\Python27\lib\json\\uuu init\uuu.py”,第250行,在dumps中
>sort_keys=sort_keys,**kw).encode(obj)文件“C:\Python27\lib\json\encoder.py”,第207行,在encode中
>chunks=self.iterencode(o,_one_shot=True)文件“C:\Python27\lib\json\encoder.py”,第270行,在iterencode中
>默认情况下,返回_iterencode(o,0)文件“C:\Python27\lib\json\encoder.py”,第184行
>raise TypeError(repr(o)+“不可JSON序列化”)TypeError:不可JSON序列化
> >>>
我对python和JSON都很陌生,因此非常感谢您的帮助
谢谢您不能直接序列化对象。请尝试以下方法:
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)
同样,对于路线
模型也是如此
如果您处理的是一系列对象,则始终可以执行以下操作:
json.dumps([city.to_json() for city in air_map.cities], outfile)
如果
air\u map.cities
是一个城市列表,他可能只需要def to_json(self):返回self即可。谢谢你指出这一点。如果序列化对象中需要所有字段,这绝对是一个很好的实现方法。我将代码更新为: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()****outfile.close()有效吗****这看起来是错误的,根据您显示的输出(error messag),您应该尝试我向您展示的第一个示例json.dumps(air\u map.cities.to\u json(),outfile)
只是您的类不可json序列化。检查该链接:
json.dumps(air_map.cities.to_json(), outfile)
json.dumps([city.to_json() for city in air_map.cities], outfile)