Python - json.load() 和 json.loads() 的区别
JSON(JavaScript Object Notation)是一种由编程语言中的文本组成的脚本(可执行)文件,用于存储和传输数据。它是一种独立于语言的格式,非常容易理解,因为它本质上是自我描述的。 Python有一个名为 json 的内置包。在本文中,我们将看到 Json.load 和 json.loads() 方法。这两种方法都用于从带有文件的 Unicode字符串中读取和写入。
json.load()
json.load()接受一个文件对象并返回 json 对象。它用于从文件中读取 JSON 编码的数据并将其转换为Python字典并反序列化文件本身,即它接受文件对象。
Syntax: json.load(fp, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
Parameters:
fp: File pointer to read text.
object_hook: It is an optional parameter that will be called with the result of any object literal decoded.
parse_float: It is an optional parameter that will be called with the string of every JSON float to be decoded.
parse_int: It is an optional parameter that will be called with the string of every JSON int to be decoded.
object_pairs_hook: It is an optional parameter that will be called with the result of any object literal decoded with an ordered list of pairs.
例子:
首先创建json文件:
Python3
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 )
Python3
with open("data_file.json", "r") as read_content:
print(json.load(read_content))
Python3
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 )
输出:
之后,创建 json 文件,让我们使用 json.load():
蟒蛇3
with open("data_file.json", "r") as read_content:
print(json.load(read_content))
输出:
{‘name’: ‘Satyam kumar’, ‘place’: ‘patna’, ‘skills’: [‘Raspberry pi’, ‘Machine Learning’, ‘Web Development’],
’email’: ‘xyz@gmail.com’, ‘projects’: [‘Python Data Mining’, ‘Python Data Science’]}
json.loads()
json.loads()方法可用于解析有效的 JSON字符串并将其转换为Python字典。它主要用于将 JSON 数据组成的原生字符串、字节或字节数组反序列化为Python字典。
Syntax: json.loads(s, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
Parameters:
s: Deserialize str (s) instance containing a JSON document to a Python object using this conversion table.
object_hook: It is an optional parameter that will be called with the result of any object literal decoded.
parse_float: It is an optional parameter that will be called with the string of every JSON float to be decoded.
parse_int: It is an optional parameter that will be called with the string of every JSON int to be decoded.
object_pairs_hook: It is an optional parameter that will be called with the result of any object literal decoded with an ordered list of pairs.
例子:
蟒蛇3
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 )
输出:
{‘Name’: ‘Jennifer Smith’, ‘Contact Number’: 7867567898, ‘Email’: ‘jen123@gmail.com’,
‘Hobbies’: [‘Reading’, ‘Sketching’, ‘Horse Riding’]}