表格如何给网站做链接,搜索引擎优化实训报告,个人网站怎么申请注册,网站之间如何做视频交换Python JSON 序列化以及反序列化
JSON (JavaScript Object Notation) 是一种轻量级的文本数据存储格式。JSON 数据通常存储在字符串中#xff0c;即JSON字符串#xff0c;其实就是一字符串#xff0c;只是带有一定的格式#xff0c;可以被解析。本文使用的 Python 版本为3…Python JSON 序列化以及反序列化
JSON (JavaScript Object Notation) 是一种轻量级的文本数据存储格式。JSON 数据通常存储在字符串中即JSON字符串其实就是一字符串只是带有一定的格式可以被解析。本文使用的 Python 版本为3.12。
反序列化 将JSON字符串解析为Python对象叫做JSON的反序列化也叫做JSON的解码。 反序列化一般使用json模块的loads与load方法。 loads当中的s并不是复数的意思而是指处理的对象类型为str bytes 和 bytearray。 load方法处理的对象类型为file like obj。 以上两个方法根据输入数据的不同返回不同的Python对象。具体的转换关系见下表 JSON 字符串Python 对象objectdictarrayliststringstrnumber(integer)intnumber(real)floatfalseFalsetrueTruenullNone
字符串的反序列化 str bytes 和 bytearray的反序列化使用方法loads。 # -*- coding:utf-8 -*-
import jsonjson_obj_str {number: 88888888, 名字: 小明}
# encode 方法将 字符串 转为 bytes
# decode 方法将 bytes 转为 字符串
json_obj_bytes json_obj_str.encode(encodingUTF-8)
json_array_str [1, 2, 3, hello]
json_str hello
json_int_num_str 6666
json_float_num_str 888.888
json_true_str true
json_false_str false
json_none_str nulldef json_str_decode(arg):python_obj json.loads(arg)print(fvalue: {python_obj}, type: {type(python_obj)})for tmp in [json_obj_str, json_obj_bytes, json_array_str, json_str, json_int_num_str, json_float_num_str,json_true_str, json_false_str, json_none_str]:json_str_decode(tmp)
输出为:
value: {number: 88888888, 名字: 小明}, type: class dict
value: {number: 88888888, 名字: 小明}, type: class dict
value: [1, 2, 3, hello], type: class list
value: hello, type: class str
value: 6666, type: class int
value: 888.888, type: class float
value: True, type: class bool
value: False, type: class bool
value: None, type: class NoneTypejson 文件的反序列化 文件的反序列化使用方法load。 # -*- coding:utf-8 -*-
import json
test.json 文件内容如下
{名字: 小明,number: 888888,女朋友: null
}
# 当 json 文件中含有中文时得指定编码为 UTF-8
with open(test.json, r, encodingUTF-8) as f:python_obj json.load(f)print(fvalue: {python_obj}, type: {type(python_obj)})
输出为
value: {名字: 小明, number: 888888, 女朋友: None}, type: class dict序列化
将Python对象转为JSON字符串叫做JSON的序列化也叫做JSON的编码。序列化一般使用json模块的dumps与dump方法。dumps当中的s并不是复数的意思而是指字符串即将Python对象编码为字符串。dump方法将Python对象编码为字符串并写入file like obj中。
Python 对象的序列化 Python对象的序列化使用方法dumps。 # -*- coding:utf-8 -*-
import jsonpy_obj_dict {number: 88888888, 名字: 小明}
py_obj_array [1, 2, 3, hello]
py_obj_str hello
py_obj_int 6666
py_obj_float 888.888
py_obj_true True
py_obj_false False
py_obj_none Nonedef json_str_encode(arg):# 当包含中文时需指定 ensure_asciiFalsejson_str json.dumps(arg, ensure_asciiFalse)print(fvalue: {json_str}, type: {type(json_str)})for tmp in [py_obj_dict, py_obj_array, py_obj_str, py_obj_int, py_obj_float,py_obj_true, py_obj_false, py_obj_none]:json_str_encode(tmp)
输出为
value: {number: 88888888, 名字: 小明}, type: class str
value: [1, 2, 3, hello], type: class str
value: hello, type: class str
value: 6666, type: class str
value: 888.888, type: class str
value: true, type: class str
value: false, type: class str
value: null, type: class strjson 文件的序列化 文件的序列化使用方法dump。 # -*- coding:utf-8 -*-
import jsonpy_obj_dict {名字: 小明, number: 888888, 女朋友: None}# 当包含中文时须同时指定 encodingUTF-8 以及 ensure_asciiFalse
with open(test.json, w, encodingUTF-8) as f:# indent2 会使得输出更加优美json.dump(py_obj_dict, f, ensure_asciiFalse, indent2)