python - 将字典(数组)导出到json
我有一个字典数组,如下所示:
myDict[0] = {'date':'today', 'status': 'ok'}
myDict[1] = {'date':'yesterday', 'status': 'bad'}
我正在尝试将此数组导出到 json 文件,其中每个字典都是其自己的条目。问题是当我尝试运行时:
dump(myDict, open("test.json", "w"))
它在每个条目之前输出一个带有数字前缀的 json 文件
{"0": {"date": "today", "status": "ok"}, "1": {"date": "yesterday", "status": "bad"} }
,这显然不是合法的 json,因为我的 json 解析器(protovis)给了我错误消息
有什么想法吗? 谢谢
I have an array of dictionaries like so:
myDict[0] = {'date':'today', 'status': 'ok'}
myDict[1] = {'date':'yesterday', 'status': 'bad'}
and I'm trying to export this array to a json file where each dictionary is its own entry. The problem is when I try to run:
dump(myDict, open("test.json", "w"))
It outputs a json file with a number prefix before each entry
{"0": {"date": "today", "status": "ok"}, "1": {"date": "yesterday", "status": "bad"} }
which apparently isn't legal json since my json parser (protovis) is giving me error messages
Any ideas?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用列表代替字典;你可能使用过:
你应该使用:
PS:无论如何,这对我来说似乎是有效的 json,但它是一个对象而不是列表;也许这就是你的解析器抱怨的原因
Use a list instead of a dictionary; you probably used:
You should use:
P.S.: It seems valid json to me anyways, but it is an object and not a list; maybe this is the reason why your parser is complaining
您应该使用 JSON 序列化器...
此外,字典数组最好序列化为如下所示:
也就是说,您应该只使用 JavaScript 数组。
You should use a JSON serializer...
Also, an array of dictionaries would better serialize to something like this:
That is, you should just use a JavaScript array.