json 是一种轻量级的数据交换格式
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
‘‘‘
将字符串转换为json串
‘‘‘
import json
test = ‘Hello World!‘
result = json.dumps(test)
print(result)
print(type(result))
结果:
linux-ko5m:/home/python/test # ./jsontest.py
"Hello World!"
<class ‘str‘>
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
‘‘‘
将列表转换为json串
‘‘‘
import json
test = [‘Hello World!‘, {‘hello‘ : "Hello World!"}]
result = json.dumps(test)
print(result)
print(type(result))
结果:
linux-ko5m:/home/python/test # ./jsontest.py
["Hello World!", {"hello": "Hello World!"}]
<class ‘str‘>
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
‘‘‘
将字字典符串转换为json串
‘‘‘
import json
test = {‘name‘:‘zhangsan‘, ‘age‘:18}
result = json.dumps(test)
print(result)
print(type(result))
结果:
linux-ko5m:/home/python/test # ./jsontest.py
{"age": 18, "name": "zhangsan"}
<class ‘str‘>
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
‘‘‘
将字字典符串转换为json串
‘‘‘
import json
test = {‘name‘:‘zhangsan‘, ‘age‘:18}
result = json.dumps(test,sort_keys=False)
print(result)
print(type(result))
结果:
linux-ko5m:/home/python/test # ./jsontest.py
{"name": "zhangsan", "age": 18}
<class ‘str‘>
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
‘‘‘
定义json串缩进
‘‘‘
import json
test = {‘name‘:‘zhangsan‘, ‘age‘:18, ‘list‘: [‘a‘, ‘b‘, ‘c‘]}
result = json.dumps(test,indent=4)
print(result)
print(type(result))
结果:
linux-ko5m:/home/python/test # ./jsontest.py
{
"name": "zhangsan",
"age": 18,
"list": [
"a",
"b",
"c"
]
}
<class ‘str‘>
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
‘‘‘
支持中文
‘‘‘
import json
test = {‘姓名‘:‘张三‘,‘年龄‘:18}
result = json.dumps(test, sort_keys=False, ensure_ascii=False)
print(result)
结果:
linux-ko5m:/home/python/test # ./jsontest.py
{"姓名": "张三", "年龄": 18}
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
‘‘‘
将json串写入文件
‘‘‘
import json
with open(‘file.json‘, ‘w‘) as f:
test = {‘name‘:‘zhangsan‘, ‘age‘:18, ‘list‘: [‘a‘, ‘b‘, ‘c‘]}
result = json.dumps(test, indent=4)
f.write(result)
结果:
linux-ko5m:/home/python/test # cat file.json
{
"list": [
"a",
"b",
"c"
],
"age": 18,
"name": "zhangsan"
}
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
‘‘‘
将json串写入文件(json.dump())
‘‘‘
import json
with open(‘file.json‘, ‘w‘) as f:
test = {‘name‘:‘zhangsan‘, ‘age‘:18, ‘list‘: [‘a‘, ‘b‘, ‘c‘]}
result = json.dump(test, f, indent=4)
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import json
with open(‘file.json‘, ‘r‘) as f:
test = f.read()
result = json.loads(test)
print(type(test))
print(‘json串:%s‘%test)
print(‘--------------------------------‘)
print(type(result))
print(‘python对象:%s‘%result)
结果:
linux-ko5m:/home/python/test # ./jsontest.py
<class ‘str‘>
json串:{
"list": [
"a",
"b",
"c"
],
"age": 18,
"name": "zhangsan"
}
--------------------------------
<class ‘dict‘>
python对象:{‘list‘: [‘a‘, ‘b‘, ‘c‘], ‘age‘: 18, ‘name‘: ‘zhangsan‘}
2. json.load()
python #!/usr/bin/python3 # -*- coding: UTF-8 -*- ‘‘‘ ‘‘‘ import json with open(‘file.json‘, ‘r‘) as f: result = json.load(f) print(type(result)) print(‘python对象:%s‘%result)
结果:
linux-ko5m:/home/python/test # ./jsontest.py
<class ‘dict‘>
python对象:{‘age‘: 18, ‘list‘: [‘a‘, ‘b‘, ‘c‘], ‘name‘: ‘zhangsan‘}
3. dumps,loads与dump,load的区别
1. dumps 和 dump 的区别
python dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,allow_nan=True, cls=None, indent=None, separators=None,default=None, sort_keys=False, **kw)
python dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,allow_nan=True, cls=None, indent=None, separators=None,default=None, sort_keys=False, **kw)
可以看到,dump比 dumps多了一个参数 fp
dumps 会直接将生成的 json串 返回,也就是可以采取 json_str=dumps()的方式来获取结果
但是 dump 没有返回值,它会将生成的 json串 输出到 fp 流中
2. loads 和 load 的区别
python load(fp, cls=None, object_hook=None, parse_float=None,parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
python loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
可以看到 load 的前两个参数是 fp 以及 cls,而 loads 的前两个参数是 s 和 encoding。
loads 会将 json串直接进行解码输出。
load 是从 fp 文件流中读取json串,然后进行解码输出。本质load函数定义的时候还是调用的 load函数。
原文:https://www.cnblogs.com/jingxindeyi/p/12927368.html