Giải tuần tự hóa python json

JSON là viết tắt của Ký hiệu đối tượng JavaScript. Nó là một định dạng mã hóa dữ liệu ở định dạng chuỗi. JSON là ngôn ngữ độc lập và do đó, nó được sử dụng để lưu trữ hoặc truyền dữ liệu trong tệp. Việc chuyển đổi dữ liệu từ một chuỗi đối tượng JSON được gọi là tuần tự hóa và chuỗi ngược lại của nó được gọi là giải tuần tự hóa. Đối tượng JSON được xác định bằng dấu ngoặc nhọn{} và bao gồm một cặp khóa-giá trị. Điều quan trọng cần lưu ý là khóa đối tượng JSON là một chuỗi và giá trị của nó có thể là bất kỳ nguyên hàm nào (e). g. int, string, null) hoặc các kiểu dữ liệu phức tạp (ví dụ:. g. mảng)

Show

Ví dụ về đối tượng JSON

{
 "id":101,
 "company" : "GeeksForGeeks"
}

Các đối tượng JSON phức tạp là những đối tượng chứa một đối tượng lồng nhau bên trong đối tượng kia. Ví dụ về đối tượng JSON phức tạp

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}

Tuần tự hóa & Giải tuần tự hóa

Python và mô-đun JSON đang hoạt động rất tốt với từ điển. Python “__dict__” có thể được sử dụng để tuần tự hóa và giải tuần tự hóa các đối tượng JSON. Có __dict__ trên bất kỳ đối tượng Python nào, là từ điển được sử dụng để lưu trữ các thuộc tính (có thể ghi) của đối tượng. Chúng tôi có thể sử dụng nó để làm việc với JSON và nó hoạt động tốt.
Mã.

Python3




{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
09
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
10

 

 

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
11
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
0
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
1
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
3
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
4
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
5
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
6
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
7
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
8
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
9
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
8
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
42
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
6
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
44
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
45
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
46

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
42
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
6
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
49
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
45
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
31

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
42

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
33
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
45
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
35_______145
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
37
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
38____145
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
20
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
21

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
22
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
45
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
24

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
25
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
26

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
25
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
28
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
29
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
29
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
31

đầu ra.  

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
4

Ghi chú. Dấu hoa thị kép ** trong GFG_User ( ** json ). Dòng tải ( json_data ) có thể trông khó hiểu. Nhưng tất cả những gì nó làm là mở rộng từ điển

Đối tượng phức tạp

Giờ đây, mọi thứ trở nên rắc rối khi xử lý các đối tượng JSON phức tạp vì thủ thuật “__dict__” của chúng tôi không còn hiệu quả nữa.
Mã.

Python3




{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
32
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
33____109
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
35

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
09
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
10

 

 

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
11
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
39____11
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
2

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
3
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
4
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
5
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
6
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
7
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
8
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
9
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
8
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
42
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
6
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
44
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
45
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
46

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
42
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
6
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
49
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
45
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
31

 

 

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
11
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
102____11
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
3
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
4
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
5
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
6
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
109
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
35
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
111

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
42
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
6
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
114
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
45
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
116

 

 

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
117
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
45
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
119
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
45
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
01
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
38
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
45
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
04
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
21

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
06
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
45
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
119
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
45
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
10
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
38
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
45
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
13
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
21

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
15
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
45
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
17
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
45
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
19

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
22
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
45
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
22
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
45
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
24
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
21

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
25
{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}
26

đầu ra.  

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}

Nhưng nếu bạn xem tài liệu về chức năng kết xuất, bạn sẽ thấy có một cài đặt mặc định mà chúng ta có thể sử dụng. Đơn giản bằng cách thay thế dòng này