参考链接: json — JSON 编码和解码器
代码实验展示1:
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import json
>>> info = '123'
>>> infoEncoded = json.dumps(info)
>>> infoEncoded
'"123"'
>>> type(infoEncoded)
<class 'str'>
>>> infoDecoded = json.loads(infoEncoded)
>>> infoDecoded
'123'
>>> type(infoDecoded)
<class 'str'>
>>>
>>>
>>> info = [0,1,2,'3']
>>> infoEncoded = json.dumps(info)
>>> infoEncoded
'[0, 1, 2, "3"]'
>>> type(infoEncoded)
<class 'str'>
>>> infoDecoded = json.loads(infoEncoded)
>>> infoDecoded
[0, 1, 2, '3']
>>> type(infoDecoded)
<class 'list'>
>>>
>>>
>>>
>>>
>>> info = {0,'asdf'}
>>> infoEncoded = json.dumps(info)
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
infoEncoded = json.dumps(info)
File "D:\Python\Python37\lib\json\__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "D:\Python\Python37\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "D:\Python\Python37\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "D:\Python\Python37\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type set is not JSON serializable
>>>
>>>
>>>
>>> info = {0:'asdf',1:'jkl;',2:[1,2,3],20200910:'abcdef'}
>>> info
{0: 'asdf', 1: 'jkl;', 2: [1, 2, 3], 20200910: 'abcdef'}
>>> infoEncoded = json.dumps(info)
>>> infoEncoded
'{"0": "asdf", "1": "jkl;", "2": [1, 2, 3], "20200910": "abcdef"}'
>>> type(infoEncoded)
<class 'str'>
>>> infoDecoded = json.loads(infoEncoded)
>>> infoDecoded
{'0': 'asdf', '1': 'jkl;', '2': [1, 2, 3], '20200910': 'abcdef'}
>>> type(infoDecoded)
<class 'dict'>
>>>
>>>
>>>
代码实验展示2:
import json
info = {0:'a',1:'b',2:'c',2020:'0910',3:[0,1,2,3],4:['',1,'A',"B"]}
print("info".center(50,'-'))
print(info)
file = open('info.json','w+')
json.dump(info,file)
# 改变当前文件操作指针的位置,file.seek(offset), offset=0表示文件开头,offset=1表示当前位置,offset=2表示文件结尾
file.seek(0)
infoDecoded = json.load(file)
print()
print("infoDecoded".center(50,'-'))
print(infoDecoded)
file.close()
控制台下输出结果展示:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
加载个人及系统配置文件用了 852 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\my-experiments> conda activate pytorch_1.7.1_cu102
(pytorch_1.7.1_cu102) PS C:\Users\chenxuqi\Desktop\News4cxq\my-experiments> & 'D:\Anaconda3\envs\pytorch_1.7.1_cu102\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '56282' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\my-experiments\test.py'
-----------------------info-----------------------
{0: 'a', 1: 'b', 2: 'c', 2020: '0910', 3: [0, 1, 2, 3], 4: ['', 1, 'A', 'B']}
-------------------infoDecoded--------------------
{'0': 'a', '1': 'b', '2': 'c', '2020': '0910', '3': [0, 1, 2, 3], '4': ['', 1, 'A', 'B']}
(pytorch_1.7.1_cu102) PS C:\Users\chenxuqi\Desktop\News4cxq\my-experiments>
生成文件内容: