如何在没有唯一键的情况下格式化json.dump()文件?

时间:2014-07-14 16:27:40

标签: json django parsing

我正在尝试将django数据库转储到json文件中。 json文件需要采用以下格式才能使我的html表实现起作用:

[{"title": "hello world", "author": "John Doe","price": 0}]

当我呼叫json.dump()时,我收到一个自动生成的密钥字典,其中包含另一个字典作为其值,如下所示:

{"1": {"price": 20, "title": "78BBOT", "author": "AT5L89"}}

这是我当前用于生成转储的django / python代码:

data = Book.objects.values('title', 'author', 'price')
with open('data.json', 'w') as outfile:
     json.dump(data, outfile)

有没有办法可以用第一个例子的格式解析这些信息?

1 个答案:

答案 0 :(得分:0)

只需忽略JSON转储中的密钥:

json.dump(data.values(), outfile)
相关问题