Python序列化之Json基础

python的序列化就是将python的基本对象转换为字符串的过程,反之则是反序列化。

序列化类型:
-> import json
import pickle

序列化定义:
序列化:对象、列表、字典都是python的基本数据类型,序列化其实就是把这些数据类型转换为字符串。
反序列化:将序列化后得到的字符串转反序列化成python的数据对象、列表、字典等类型

json的作用:
在python的基本数据类型与字符串之间进行相互转换的作用

json.dumps()函数:
将python基本数据类型转换成字符串类型,称为序列化

json.loads()函数:
将字符串形式转换成python的基本数据类型,称为反序列化

json.loads()条件:
虽然loads函数可以将字符串发序列化成python基本数据类型,但是字符串必须是
正规的python基本数据类型,不能是类似{"k1":123]这样,既不是字典,又不是列表。

requests.get('URL')
获取URL指定的资源,http://wthrcdn.etouch.cn/weather_mini?city=上海 获取上海天气的API

代码部分:
import json # 做序列化和反序列化需要用到json模块
import requests  #请求URL资源,需要用到requests模块

print('\n','序列化'.center(40,'-'))
dic = {'k1':'v1'}
print(dic,type(dic))
# 输出结果:{'k1': 'v1'} <class 'dict'>

# json.dumps(python数据类型)就可以把数据类型转换为字符串,这个过程就是序列化
result = json.dumps(dic)
print(result,type(result))
# 输出结果:{"k1": "v1"} <class 'str'>

print('\n','反序列化'.center(40,'-'))
#json.loads(字符串)就可以把字符串转换成python的基本数据类型,这个过程就是反序列号
s1 = '{"k1":123}' # s1虽然看起来像字典,但它是一个字符串
dic = json.loads(s1)
print(dic,type(dic))
# 输出结果: {'k1': 123} <class 'dict'>

print('\n','基于天气API获取python的json数据'.center(40,'-'))
response = requests.get('http://wthrcdn.etouch.cn/weather_mini?city=上海')
response.encoding = 'utf-8'
print(response.text,type(response.text))    # 获取到的结果是字符串类型
# 输出结果: {"data":{"yesterday":{"date":"26日星期一"...    <class 'str'>

# 通过json.loads()函数将字符串结果转换为字典类型,转换成字典后对字典内对数据进行操作就方便的多了。
dic = json.loads(response.text)
print(dic,type(dic))    # json.loads()转换后的结果为python的基本数据类型,这里就是字典。
# 输出结果: {'data': {'yesterday': {'date': '26日星期一...      <class 'dict'>

执行结果:

------------------序列化-------------------
{'k1': 'v1'} <class 'dict'>
{"k1": "v1"} <class 'str'>

 ------------------反序列化------------------
{'k1': 123} <class 'dict'>

 ---------基于天气API获取python的json数据---------
{"data":{"yesterday":{"date":"26日星期一","high":"高温 28℃","fx":"西风","low":"低温 23℃","fl":"微风","type":"雷阵雨"},"city":"上海","aqi":"107","forecast":[{"date":"27日星期二","high":"高温 29℃","fengli":"微风级","low":"低温 23℃","fengxiang":"西风","type":"雷阵雨"},{"date":"28日星期三","high":"高温 28℃","fengli":"微风级","low":"低温 23℃","fengxiang":"东南风","type":"小雨"},{"date":"29日星期四","high":"高温 27℃","fengli":"微风级","low":"低温 24℃","fengxiang":"东南风","type":"中雨"},{"date":"30日星期五","high":"高温 32℃","fengli":"微风级","low":"低温 26℃","fengxiang":"东南风","type":"多云"},{"date":"1日星期六","high":"高温 32℃","fengli":"4-5级","low":"低温 26℃","fengxiang":"东南风","type":"多云"}],"ganmao":"各项气象条件适宜,无明显降温过程,发生感冒机率较低。","wendu":"24"},"status":1000,"desc":"OK"} <class 'str'>

{'data': {'yesterday': {'date': '26日星期一', 'high': '高温 28℃', 'fx': '西风', 'low': '低温 23℃', 'fl': '微风', 'type': '雷阵雨'}, 'city': '上海', 'aqi': '107', 'forecast': [{'date': '27日星期二', 'high': '高温 29℃', 'fengli': '微风级', 'low': '低温 23℃', 'fengxiang': '西风', 'type': '雷阵雨'}, {'date': '28日星期三', 'high': '高温 28℃', 'fengli': '微风级', 'low': '低温 23℃', 'fengxiang': '东南风', 'type': '小雨'}, {'date': '29日星期四', 'high': '高温 27℃', 'fengli': '微风级', 'low': '低温 24℃', 'fengxiang': '东南风', 'type': '中雨'}, {'date': '30日星期五', 'high': '高温 32℃', 'fengli': '微风级', 'low': '低温 26℃', 'fengxiang': '东南风', 'type': '多云'}, {'date': '1日星期六', 'high': '高温 32℃', 'fengli': '4-5级', 'low': '低温 26℃', 'fengxiang': '东南风', 'type': '多云'}], 'ganmao': '各项气象条件适宜,无明显降温过程,发生感冒机率较低。', 'wendu': '24'}, 'status': 1000, 'desc': 'OK'} <class 'dict'>

 

转载于:https://www.cnblogs.com/tdcqma/p/7086872.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值