b = b"demo" s = "demo" # 字符串转字节 s = bytes(s, encoding = "utf8") s = str.encode(s) # 字节转字符串 s = str(b, encoding = "utf8") s = bytes.decode(b)
json 序列与反序列化
''' Supports the following objects and types by default:
python支持的Json类型 +-------------------+---------------+ | Python | JSON | +===================+===============+ | dict | object | {} +-------------------+---------------+ | list, tuple | array |[]() +-------------------+---------------+ | str | string |"" +-------------------+---------------+ | int, float | number |124 +-------------------+---------------+ | True | true | +-------------------+---------------+ | False | false | +-------------------+---------------+ | None | null | +-------------------+---------------+ '''
name = {'name': "gggg"} name01 = """{"name": "gggg"}""" # 转化成字符串(序列化) name_dumps = json.dumps(name) # 转化成json格式(反序列化) name01_loads = json.loads(name01)
# json.dumps 序列化时对中文默认使用的ascii编码.想输出真正的中文需要指定ensure_ascii=False:
print(type(name_dumps)) print(type(name01_loads))
js中处理json
序列化:
Json.stringify()
反序列化
Json.parse()