0
我有一些困难,从相当复杂的python对象构建JSON文件。构建JSON文件的python对象
我正努力打造成为一个JSON文件中的类看起来是这样的:
class Recipe:
def __ini__(self):
self.recipeTitle = ""
self.recipeDiscription = ""
self.recipeMetaData = []
self.reipeIngredients = collections.OrderedDict()
self.instructions = []
self.searchableIngredients = []
self.allIng = []
self.tags = []
self.ingredientMetadata = []
# self.getAllIngredients()
我试图建立一个字典,其中键是属性的字符串名称,和值是内容属性。但是,json.dump()不喜欢列表,字典和字符串的字典。我是否需要手动创建填充JSON文件的逻辑,例如通过连接列表中的所有内容并以独特方式对其进行分隔来创建字符串,还是将此类对象转换为JSON更简单?
作为参考,填充配方对象会是这个样子:
recipe.recipeTitle = "Pancakes"
recipe.recipeDiscription = "Something really tasty"
recipe.metaData = ["15 Minutes", "Serves 5"]
recipe.recipeIngredients = {('For the sauce', ['sugar', 'spice',
'everything nice']),('For the food', ['meat', 'fish', 'eggs'])}
recipe.instructions = ['First, stir really well', 'Second - fry']
self.allIng = ['sugar', 'spice', 'everything nice', 'meat', 'fish',
'eggs']
self.tags = [1252, 2352, 2174, 454]
self.ingredientMetadata = [1,17,23,55,153,352]
我卡试图把它变成JSON和任何帮助将不胜感激!
提前致谢。