
JSON介绍:json.dump()、json.dumps()、json.load()、json.loads()
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
·
import json 首先导入json
一. 函数用法
1.json.dumps():将Python数据结构转换为JSON,即dict类型转成str类型。
-
obj:需要转化成json的对象
-
sort_keys:(布尔值)编码器按照字典排序(a到z)输出
-
indent:根据数据格式缩进显示
-
separators:设置分隔符
-
skipkeys:(布尔值) 检查python的基本类型
-
ensure_ascii : 输出是否ASCLL码
-
check_circular:容器类型的循环引用检查
import json
data = {'name' : 'Shang','age' : 20}
json_str = json.dumps(data,ensure_ascii=False)
print(json_str)
# 输出为
{"name": "Shang", "age": 20}
2.json.dump():编码,用于将dict类型的数据转成str类型,并写入到json文件。
import json
data = {'name':'Shang','age':20}
#将python编码成json放在指定的文件里
filename = 'a.txt'
with open (filename,'w') as f:
json.dump(data ,f)
3.json.loads():将JSON编码的字符串转换回Python数据结构,即str类型转换成dict类型。
import json
data = {'name':'Shang','age':20}
data = json.dumps(data) # 将python数据结构转为json类型
print(json.loads(data)) # 将json类型再转为python数据结构
# 输出为
{'name': 'name', 'age': 20}
4.json.load():解码,用于从json文件中读取数据。
import json
data = {'name':'Shang','age':20}
filename = 'a.txt'
with open (filename,'w') as f:
json.dump(data,f)
with open (filename) as f_1:
print(json.load(f_1))
参考原文链接:https://blog.csdn.net/zhu_rui/article/details/123025943
适用于现代 C++ 的 JSON。
最近提交(Master分支:5 个月前 )
3a570393
Bumps [cppcheck](https://github.com/msclock/cppcheck-wheel) from 1.4.8 to 1.5.0.
- [Release notes](https://github.com/msclock/cppcheck-wheel/releases)
- [Changelog](https://github.com/msclock/cppcheck-wheel/blob/master/.releaserc.js)
- [Commits](https://github.com/msclock/cppcheck-wheel/compare/v1.4.8...v1.5.0)
---
updated-dependencies:
- dependency-name: cppcheck
dependency-type: direct:production
update-type: version-update:semver-minor
...
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 2 天前
4d216e0c
Bumps [actions/upload-artifact](https://github.com/actions/upload-artifact) from 4.6.1 to 4.6.2.
- [Release notes](https://github.com/actions/upload-artifact/releases)
- [Commits](https://github.com/actions/upload-artifact/compare/4cec3d8aa04e39d1a68397de0c4cd6fb9dce8ec1...ea165f8d65b6e75b540449e92b4886f43607fa02)
---
updated-dependencies:
- dependency-name: actions/upload-artifact
dependency-type: direct:production
update-type: version-update:semver-patch
...
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 2 天前
更多推荐




所有评论(0)