温州网站关键词排名优化,行业门户网站建设费用,网站建设图库,wordpress胖子马在 Python 中#xff0c;可以使用 json 模块将字典转换为 JSON 格式的字符串。该模块提供了 json.dumps() 方法#xff0c;用于将 Python 对象#xff08;如字典、列表#xff09;序列化为 JSON 字符串。 1、问题背景
用户想要将一个 Python 字典转换为 JSON 格式#xf…在 Python 中可以使用 json 模块将字典转换为 JSON 格式的字符串。该模块提供了 json.dumps() 方法用于将 Python 对象如字典、列表序列化为 JSON 字符串。 1、问题背景
用户想要将一个 Python 字典转换为 JSON 格式但是遇到了一个错误错误信息提示对象 City 和 Route 不可序列化。
以下是他尝试的代码
class City:Stores city infodef __init__(self, code, name, country, continent, timezone, coordinates, population, region):self.code codeself.name nameself.country countryself.continent continentself.timezone timezoneself.coordinates coordinatesself.population populationself.region regiondef 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}class Route:Stores route infodef __init__(self, src, dest, dist):self.flight_path src - destself.src srcself.dest destself.dist distdef to_json(self):return {source: self.src, destination: self.dest, distance: self.dist}def map_to_json(my_file, air_map):Saves JSON Datawith 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()2、解决方案
为了解决问题用户需要使用 to_json() 方法将每个对象转换为一个字典然后再使用 json.dumps() 方法将字典转换为 JSON 格式。 以下是修改后的代码
class City:Stores city infodef __init__(self, code, name, country, continent, timezone, coordinates, population, region):self.code codeself.name nameself.country countryself.continent continentself.timezone timezoneself.coordinates coordinatesself.population populationself.region regiondef 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}class Route:Stores route infodef __init__(self, src, dest, dist):self.flight_path src - destself.src srcself.dest destself.dist distdef to_json(self):return {source: self.src, destination: self.dest, distance: self.dist}def map_to_json(my_file, air_map):Saves JSON Datawith 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()
city1 City(ABC, City1, Country1, Continent1, Timezone1, Coordinates1, 100000, Region1)
city2 City(DEF, City2, Country2, Continent2, Timezone2, Coordinates2, 200000, Region2)
city3 City(GHI, City3, Country3, Continent3, Timezone3, Coordinates3, 300000, Region3)
route1 Route(ABC,DEF, 100)
route2 Route(DEF,GHI, 200)
air_map.cities[ABC] city1
air_map.cities[DEF] city2
air_map.cities[GHI] city3
air_map.routes[ABC-DEF] route1
air_map.routes[DEF-GHI] route2map_to_json(map.json, air_map)运行该代码后就可以将字典转换为 JSON 格式并保存到文件中。
上面就是今天我要讲的全部内容详细并完整的记录了如果有任何问题大家都可以联系我。