一、图解
- 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)
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/213500.html