'''
序列化:把python的数据类型转为str类型
反序列化:把str类型转为python的数据类型
'''
一、字典的序列化与反序列化
'''字典的序列化与反序列化过程'''
# 字典的序列化
dict1={'name':'xingxing','age':18,'address':'wuhan'}
dict1_str=json.dumps(dict1)
print(dict1_str,type(dict1_str))
# 字典的反序列化
str_dict1 = json.loads(dict1_str)
print(str_dict1,type(str_dict1))
二、列表的序列化与反序列化过程
'''列表的序列化与反序列化过程'''
list1 = ['xingxing','18','wuhan']
list1_str = json.dumps(list1)
print(list1_str,type(list1_str))
str_list1 = json.loads(list1_str)
print(str_list1,type(str_list1))
三、 元组的序列化与反序列化过程
'''元组的序列化与反序列化过程'''
tuple1 =('xingxing','18','wuhan1')
tuple1_str = json.dumps(tuple1)
print(tuple1_str,type(tuple1_str))
str_tuple1 = json.loads(tuple1_str)
print(str_tuple1,type(str_tuple1))
四、实例应用
r = requests.post(url='http://iop.parkingwang.com/api/iop/auth/login',
json={
"username": "12",
"password": "c20ad4d76fe97759aa27a0c99bff6710",
"rsa_password": "NPsnpevDFIlzm1uMv5olh/Lbs7OJ54cGVSc0s9XNVuByIZBhUo6RzF/2PsY1tTLYsT8TTKdQkguh00PiZbsREhuUnh0vum0s/bg243Mzdv3aEQscXRXpkWs5PWtJuC+JVQPofH5XcyfqMbmOa677hDiYH2yg9w+pxsaftMsHpJfuviHJ5FXBk2/F46hGJWMOXdDV2dDwZeRNd1HPcGFW0bO09KafW0w1Tc+Zui49ZkR8YYAWWfNzQ/vzKUEtyREAlIE94G2ytglSTQKVYzR84yKVoCQw23g8hkltrNZywn0m6v3k2OY6RGx6on4KJikwAujAnRP0zQ8xJCqjuM6tCgfFzG6rMMMAf5LlQQFnBUpEFK+G+Ah4TSXkPB7hGFBVEKvE2NpUOohtZqhungZJxTpb1EyOixFyCbLo9SbSE6inkeOQrivOH3g09luStpHtPxxD539jzujn02/cCSNW7D/7W119F8Yi4R91ttJ7jKTo+Cjt6CGr9q3ZykPm4QSYlQX0Jq+ReyQcro7Ql7BjYgAxzwj0KHoHgjIDUt80yv4dF0flsj8089KUtMx2ddWGkhcFSl7QcRCyuYe6z77Gy+bl+X7P8YfI2HPAMBczT88zQhtMVQ2H+uvgDn5+mlLFAHAws5raey9e2AeTi2IIqCWiEoJEF93G/RpjHmf+Uk0="
},
headers={'Content-Type':'application/json;charset=utf-8'
}
)
print(r.status_code)
print(r.text)
print(json.loads(str(r.text)))
print(json.loads(str(r.text))['message'])
实例结果:
200
{"code":"1014","message":"\u7528\u6237\u540d\u6216\u8005\u5bc6\u7801\u9519\u8bef"}
{'code': '1014', 'message': '用户名或者密码错误'}
用户名或者密码错误