Python中json.load()和json.loads()的区别
一、图解
- 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()用法
例子
import json
# JSON string:
# Multi-line string
data = """{
"Name": "Jennifer Smith",
"Contact Number": 7867567898,
"Email": "jen123@gmail.com",
"Hobbies":["Reading", "Sketching", "Horse Riding"]
}"""
#学习中遇到问题没人解答?小编创建了一个Python学习交流群:711312441
# parse data:
res = json.loads(data)
# the result is a Python dictionary:
print(res)