easyidp.jsonfile.dict2json

easyidp.jsonfile.dict2json(data_dict, json_path, indent=None, encoding='utf-8')

将字典对象保存为相同结构的 JSON 文件

参数:
  • data_dict (dict) -- 要保存为 JSON 文件的字典对象

  • json_path (str) -- 保存 JSON 文件的路径,包括文件名,例如 D:/xxx/xxxx/save.json

  • indent (int | None) -- 是否以缩进保存“可读”JSON,默认 0 无缩进

  • encoding (str) -- 输出文件的编码类型

示例

>>> import easyidp as idp
>>> a = {"test": {"rua": np.asarray([[12, 34], [45, 56]])}, "hua":[np.int32(34), np.float64(34.567)]}
>>> idp.jsonfile.dict2json(a, "/path/to/save/json_file.json")

备注

无缩进的字典:

>>> print(json.dumps(data), indent=0)
{"age": 4, "name": "niuniuche", "attribute": "toy"}

使用 4 个空格缩进的字典:

>>> print(json.dumps(data,indent=4))
{
    "age": 4,
    "name": "niuniuche",
    "attribute": "toy"
}