JSON [ký hiệu đối tượng JavaScript] là tệp tập lệnh [thực thi] được tạo bằng văn bản bằng ngôn ngữ lập trình, được sử dụng để lưu trữ và chuyển dữ liệu. Nó là một định dạng độc lập với ngôn ngữ và rất dễ hiểu vì nó tự mô tả trong tự nhiên. Python có một gói tích hợp có tên là JSON. Trong bài viết này, chúng ta sẽ xem các phương thức json.load và json.loads []. Cả hai phương thức đều được sử dụng để đọc và ghi từ chuỗi Unicode với tệp. & NBSP;
json.load[]
json.load [] lấy một đối tượng tệp và trả về đối tượng JSON. Nó được sử dụng để đọc dữ liệu được mã hóa JSON từ một tệp và chuyển đổi nó thành từ điển Python và giải phóng một tệp, tức là nó chấp nhận một đối tượng tệp. takes a file object and returns the json object. It is used to read JSON encoded data from a file and convert it into a Python dictionary and deserialize a file itself i.e. it accepts a file object.
Cú pháp: json.load [fp, cls = none, object_hook = none, parse_float = none, parse_int = none, parse_constant = nonejson.load[fp, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw]
Parameters:
FP: Tệp con trỏ để đọc văn bản. File pointer to read text.
Object_hook: Đây là một tham số tùy chọn sẽ được gọi với kết quả của bất kỳ đối tượng nào được giải mã theo nghĩa đen. It is an optional parameter that will be called with the result of any object literal decoded.
parse_float: Đây là một tham số tùy chọn sẽ được gọi với chuỗi của mỗi float JSON để được giải mã. & nbsp;It is an optional parameter that will be called with the string of every JSON float to be decoded.
parse_int: Đó là một tham số tùy chọn sẽ được gọi với chuỗi của mỗi json int để được giải mã. It is an optional parameter thatwill be called with the string of every JSON int to be decoded.
Object_pairs_hook: Đây là một tham số tùy chọn & nbsp; sẽ được gọi với kết quả của bất kỳ đối tượng nào được giải mã với một danh sách các cặp được đặt hàng.It is an optional parameter that will be called with the result of any object literal decoded with an ordered list of pairs.
Example:
Đầu tiên tạo tệp JSON:
Python3
import
json
data
=
{
Các
json
1:
json
3,
json
6json
7
json
8json
9,
json
8data
2,
json
8data
5
data
7
data
9:
=
1,
=
4json
7
json
8=
7,
json
8{
0
{
2
{
3
{
4{
5{
6{
7 {
8{
9
0
2
Output:
data_file.json
Sau đó, tạo tệp JSON, hãy để sử dụng json.load []:
Python3
{
4{
5
5{
7{
8
8
9
"name"
1"name"
2
Output:
{'Tên': 'Satyam Kumar', 'Place': 'Patna', 'Kỹ năng': ['Raspberry Pi', 'Học máy', 'Phát triển web'], 'Email': '', 'Dự án': ['Khai thác dữ liệu Python', 'Khoa học dữ liệu Python']}
’email’: ‘’, ‘projects’: [‘Python Data Mining’, ‘Python Data Science’]}
json.loads[]
Phương thức JSON.LOADS [] có thể được sử dụng để phân tích chuỗi JSON hợp lệ và chuyển đổi nó thành từ điển Python. Nó chủ yếu được sử dụng để giải phóng chuỗi tự nhiên, byte hoặc mảng byte bao gồm dữ liệu JSON vào từ điển Python. method can be used to parse a valid JSON string and convert it into a Python Dictionary. It is mainly used for deserializing native string, byte, or byte array which consists of JSON data into Python Dictionary.
Cú pháp: json.loads [s, mã hóa = none, cls = none, object_hook = none, parse_float = none, parse_int = none, parse_constant = none, object_pair_hook = none, ** kw]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: Thể hiện Deserialize STR [S] chứa tài liệu JSON vào đối tượng Python bằng cách sử dụng bảng chuyển đổi này.Deserialize str [s] instance containing a JSON document to a Python object using this conversion table.
Object_hook: Đây là một tham số tùy chọn sẽ được gọi với kết quả của bất kỳ đối tượng nào được giải mã theo nghĩa đen. It is an optional parameter that will be called with the result of any object literal decoded.
parse_float: Đây là một tham số tùy chọn sẽ được gọi với chuỗi của mỗi float JSON để được giải mã. & nbsp;It is an optional parameter that will be called with the string of every JSON float to be decoded.
parse_int: Đó là một tham số tùy chọn sẽ được gọi với chuỗi của mỗi json int để được giải mã.It is an optional parameter thatwill be called with the string of every JSON int to be decoded.
Object_pairs_hook: Đây là một tham số tùy chọn & nbsp; sẽ được gọi với kết quả của bất kỳ đối tượng nào được giải mã với một danh sách các cặp được đặt hàng.It is an optional parameter that will be called with the result of any object literal decoded with an ordered list of pairs.
Example:
Python3
Đầu tiên tạo tệp JSON:
import
json
data
=
{
"name"
1:
1
Output:
Các
‘Hobbies’: [‘Reading’, ‘Sketching’, ‘Horse Riding’]}