一、图解
json.loads():解析一个有效的JSON字符串并将其转换为Python字典
json.load():从一个文件读取JSON类型的数据,然后转转换成Python字典
二、json.loads()用法
1、例子
import json data = { "name": "Satyam kumar", "place": "patna", "skills": [ "Raspberry pi", "Machine Learning", "Web Development" ], "email": "xyz@gmail.com", "projects": [ "Python Data Mining", "Python Data Science" ] } with open("data_file.json", "w") as write: json.dump(data, write) with open("data_file.json", "r") as read_content: print(json.load(read_content))
2、Python和Json数据类型的映射
JSON Equivalent | Python |
---|---|
object | dict |
array | list |
string | str |
number | int |
true | True |
false | False |
null | None |
三、json.load()用法
1、例子
import json # JSON string: # Multi-line string data = """{ "Name": "Jennifer Smith", "Contact Number": 7867567898, "Email": "jen123@gmail.com", "Hobbies":["Reading", "Sketching", "Horse Riding"] }""" # parse data: res = json.loads(data) # the result is a Python dictionary: print(res)
四、写在最后
李先生(Lemon),高级运维工程师(自称),SRE专家(目标),梦想在35岁买一辆保时捷。喜欢钻研底层技术,认为底层基础才是王道。一切新技术都离不开操作系统(CPU、内存、磁盘)、网络等。坚持输入输出,记录自己学习的点滴,在平凡中坚持前行,总有一天会遇见不一样的自己。公众号:运维汪(ID:Leeeee_Li)。