python字典类型与 json数据转换
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
免费下载资源
·
一、json是什么
json是轻量级的数据交换格式,是各种编程语言互通的桥梁。是一个约定好的标准,如有人用java,有人用PHP去开发一个系统,但是这两种语言不能直接互通,那就需要先转换成国际化标准的json格式。
二、python与json如何转换数据
1.将格式转换成json对象
json.dumps() 将Python 字典类型转换为 JSON 对象
代码如下(示例):
import json
data = {
'number' : 1307230000,
'name' : 'christy'
}
j_str = json.dumps(data)
print("转换成json后的数据:",j_str)
print("原始数据:",data)
输出结果:
转换成json后的数据: {"number": 1307230000, "name": "christy"}
原始数据: {'number': 1307230000, 'name': 'christy'}
2.将格式转换成python对象
json.loads() 将json格式转换成python字典类型
代码如下(示例):
import json
data = {
'number' : 1307230000,
'name' : 'christy'
}
print("原始数据:",data)
j_str = json.dumps(data)
print("转换成json后的数据:",j_str)
l_str = json.loads(j_str)
print("转换成字典类型后的数据:",l_str)
输出结果:
原始数据: {'number': 1307230000, 'name': 'christy'}
转换成json后的数据: {"number": 1307230000, "name": "christy"}
转换成字典类型后的数据: {'number': 1307230000, 'name': 'christy'}
3.json与字典区别
1、json 中除数据外,key和value都是字符串,要用双引号引起来;空值为null;布尔值为false/true(小写)
2、字典,key,value可单引号;空值为None;布尔值为False/True
import json
data = {
'number' : 1307230000,
'name' : 'christy',
'buer' : False,
"nul" : None
}
print("原始数据:",data)
j_str = json.dumps(data)
print("转换成json后的数据:",j_str)
l_str = json.loads(j_str)
print("转换成字典类型后的数据:",l_str)
输出结果:
原始数据: {'number': 1307230000, 'name': 'christy', 'buer': False, 'nul': None}
转换成json后的数据: {"number": 1307230000, "name": "christy", "buer": false, "nul": null}
转换成字典类型后的数据: {'number': 1307230000, 'name': 'christy', 'buer': False, 'nul': None}
三、总结
1, json.dumps() 将Python 字典类型转换为 JSON 对象
2, json.loads() 将json格式转换成python字典类型
3, json中除数据即数字外,key和value都是字符串,要用双引号引起来;空值为null;布尔值为false/true(小写)
GitHub 加速计划 / js / json
18
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:3 个月前 )
f06604fc
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com>
---------
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> 2 天前
d23291ba
* add a ci step for Json_Diagnostic_Positions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* Update ci.cmake to address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* address review comment
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix typo in the comment
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix typos in ci.cmake
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* invoke the new ci step from ubuntu.yml
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* issue4561 - use diagnostic positions for exceptions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_documentation check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci check failures for unit-diagnostic-postions.cpp
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* improvements based on review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix const correctness string
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* further refinements based on reviews
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* add one more test case for full coverage
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* ci check fix - add const
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* add unit tests for json_diagnostic_postions only
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_diagnostics
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_build_documentation check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
---------
Signed-off-by: Harinath Nampally <harinath922@gmail.com> 2 天前
更多推荐
已为社区贡献5条内容
所有评论(0)