📜  Python – 将字典列表转换为 JSON

📅  最后修改于: 2022-05-13 01:55:46.437000             🧑  作者: Mango

Python – 将字典列表转换为 JSON

在本文中,我们将讨论如何在Python中将字典列表转换为 JSON

方法一:使用 json.dumps()

此函数将字典列表转换为 JSON。

句法:

json.dumps(dict, indent)

参数:

  • 字典 - 应转换为 JSON 对象的字典的名称。
  • indent – 定义缩进的单位数

示例:用于创建员工数据字典列表并转换为 JSON 的Python程序

Python3
# import json module
import json
  
# list of dictionaries of employee data
data = [{"id": ("1", "2", "3"), "name": ("bhanu", "sivanagulu"), 
         "department": ("HR", "IT")},
        {"id": ("4", "5", "6"), "name": ("sai", "poori"),
         "department": ("HR", "IT")},
        {"id": ("7", "8", "9"), "name": ("teja", "gowtam"),
         "department": ("finance", "IT")},
        {"id": ("10", "11", "12"), "name": ("sai", "jyothi"),
         "department": ("business", "IT")},
        {"id": ("13", "14", "15"), "name": ("prudhvi", "nagendram"),
         "department": ("business", "IT")}]
  
  
# convert into json
final = json.dumps(data, indent=2)
  
# display
print(final)


Python3
# import json module
from google.colab import files
import json
  
# list of dictionaries of employee data
data = [{"id": ("1", "2", "3"), "name": ("bhanu", "sivanagulu"),
         "department": ("HR", "IT")},
        {"id": ("4", "5", "6"), "name": ("sai", "poori"),
         "department": ("HR", "IT")},
        {"id": ("7", "8", "9"), "name": ("teja", "gowtam"),
         "department": ("finance", "IT")},
        {"id": ("10", "11", "12"), "name": ("sai", "jyothi"),
         "department": ("business", "IT")},
        {"id": ("13", "14", "15"), "name": ("prudhvi", "nagendram"),
         "department": ("business", "IT")}]
  
  
# convert into json
# file name is mydata
with open("mydata.json", "w") as final:
    json.dump(data, final)
  
# download the json file
files.download('mydata.json')


输出

[
  {
    "id": [
      "1",
      "2",
      "3"
    ],
    "name": [
      "bhanu",
      "sivanagulu"
    ],
    "department": [
      "HR",
      "IT"
    ]
  },
  {
    "id": [
      "4",
      "5",
      "6"
    ],
    "name": [
      "sai",
      "poori"
    ],
    "department": [
      "HR",
      "IT"
    ]
  },
  {
    "id": [
      "7",
      "8",
      "9"
    ],
    "name": [
      "teja",
      "gowtam"
    ],
    "department": [
      "finance",
      "IT"
    ]
  },
  {
    "id": [
      "10",
      "11",
      "12"
    ],
    "name": [
      "sai",
      "jyothi"
    ],
    "department": [
      "business",
      "IT"
    ]
  },
  {
    "id": [
      "13",
      "14",
      "15"
    ],
    "name": [
      "prudhvi",
      "nagendram"
    ],
    "department": [
      "business",
      "IT"
    ]
  }
]

方法二:使用 json.dump()

这会将转换后的 JSON 数据写入文件。

句法:

json.dump(dict, file_pointer)

参数:

  • dictionary – 应转换为 JSON 对象的字典名称。
  • 文件指针——以写入或附加模式打开的文件的指针。

句法:

with open("mydata.json", "w") as final:
   json.dump(data, final)

其中 mydata 是新的 JSON 文件。最后,我们必须下载创建的 JSON 文件

句法:

files.download('mydata.json')

例子:

Python3

# import json module
from google.colab import files
import json
  
# list of dictionaries of employee data
data = [{"id": ("1", "2", "3"), "name": ("bhanu", "sivanagulu"),
         "department": ("HR", "IT")},
        {"id": ("4", "5", "6"), "name": ("sai", "poori"),
         "department": ("HR", "IT")},
        {"id": ("7", "8", "9"), "name": ("teja", "gowtam"),
         "department": ("finance", "IT")},
        {"id": ("10", "11", "12"), "name": ("sai", "jyothi"),
         "department": ("business", "IT")},
        {"id": ("13", "14", "15"), "name": ("prudhvi", "nagendram"),
         "department": ("business", "IT")}]
  
  
# convert into json
# file name is mydata
with open("mydata.json", "w") as final:
    json.dump(data, final)
  
# download the json file
files.download('mydata.json')

输出: