如何将嵌套字典写入json [英] How to write a nested dictionary to json

查看:129
本文介绍了如何将嵌套字典写入json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中这样创建了一个嵌套字典:

I created a nested dictionary in Python like this:

{
 "Laptop": {
            "sony": 1
            "apple": 2
            "asus": 5
          },
 "Camera": {
            "sony": 2
            "sumsung": 1
            "nikon" : 4
           },
}

但是我不知道如何将嵌套的字典写到json文件中.任何意见将不胜感激..!

But I couldn't figure out how to write this nested dict into a json file. Any comments will be appreciated..!

推荐答案

d = {
 "Laptop": {
            "sony": 1,
            "apple": 2,
            "asus": 5,
          },
 "Camera": {
            "sony": 2,
            "sumsung": 1,
            "nikon" : 4,
           },
}
with open("my.json","w") as f:
    json.dump(d,f)

这篇关于如何将嵌套字典写入json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆