- ファイルのシリアル化->サーバーの応答データをファイルに書き込みます
json.dump(r.content.decode("utf-8"), open("weather.json", "w"))
- ファイルの逆シリアル化->ファイルコンテンツの読み取り
fp=json.load(open("weather.json", "r"))
import json
import requests
"""
文件的序列化与反序列化
"""
r = requests.get(url="http://www.weather.com.cn/data/sk/101190408.html")
json.dump(r.content.decode("utf-8"), open("weather.json", "w"))
"""
1、文件反序列化后,类型是unicode
2、进行编码,把Unicode类型转换为str类型
3、然后使用反序列化把str转换为字典类型
"""
fp=json.load(open("weather.json", "r"))
print(fp,type(fp))
dict1=json.loads(fp)
print(type(dict1),dict1['weatherinfo']['city'])