Chuyển đổi JSON thành từ điển trong Python
Cải thiện bài viết
Lưu bài viết
Thích bài viết
- Độ khó. Cơ bản
- Cập nhật lần cuối. 07 tháng 12 năm 2021
Cải thiện bài viết
Lưu bài viết
JSON là viết tắt của Ký hiệu đối tượng JavaScript. Điều đó có nghĩa là một tệp script [có thể thực thi] được tạo thành từ văn bản bằng ngôn ngữ lập trình, được sử dụng để lưu trữ và truyền dữ liệu. Python hỗ trợ JSON thông qua gói tích hợp có tên là json. Để sử dụng tính năng này, chúng tôi nhập gói json trong tập lệnh Python. Văn bản trong JSON được thực hiện thông qua chuỗi trích dẫn chứa giá trị trong ánh xạ khóa-giá trị trong { }. Nó tương tự như từ điển trong Python.
Hàm được sử dụng.
- json. trọng tải[]. json. hàm loading[] có trong mô-đun 'json' tích hợp sẵn của python. Hàm này được sử dụng để phân tích cú pháp chuỗi JSON.
Cú pháp. json. tải[file_name]
Tham số. Nó lấy tệp JSON làm tham số.
Kiểu trả về. Nó trả về đối tượng từ điển python.
Ví dụ 1. Giả sử tệp JSON trông như thế này.
Chúng tôi muốn chuyển đổi nội dung của tệp này sang từ điển Python. Dưới đây là việc thực hiện.
Python3
# Python program to demonstrate
# Conversion of JSON data to
# dictionary
# importing the module
import
json
# Opening JSON file
with
open
[
# Conversion of JSON data to
0# Conversion of JSON data to
1
# Conversion of JSON data to
2# Conversion of JSON data to
3# Conversion of JSON data to
4 # Conversion of JSON data to
5
# Conversion of JSON data to
2____17
# Conversion of JSON data to
2______19[
# dictionary
1# dictionary
2# dictionary
3# dictionary
4
# Conversion of JSON data to
2____26
# Conversion of JSON data to
2______19[
# importing the module
0# importing the module
1# importing the module
2# importing the module
3
# Conversion of JSON data to
2______19[
# importing the module
7# importing the module
1# importing the module
9# importing the module
3
Đầu ra.
Ví dụ 2. Đọc dữ liệu lồng nhau
Trong tệp JSON ở trên, có một từ điển lồng nhau trong khóa đầu tiên people1. Dưới đây là việc thực hiện đọc dữ liệu lồng nhau.
Python3
# Python program to demonstrate
# Conversion of JSON data to
# dictionary
# importing the module
import
json
# Opening JSON file
with
open
[
# Conversion of JSON data to
0# Conversion of JSON data to
1
# Conversion of JSON data to
2# Conversion of JSON data to
3# Conversion of JSON data to
4 # Conversion of JSON data to
5
# Conversion of JSON data to
2____58
# Conversion of JSON data to
2# Opening JSON file
0
# Conversion of JSON data to
2______19# Opening JSON file
3# importing the module
2# Opening JSON file
5# Opening JSON file
6# importing the module
3
# Conversion of JSON data to
2
# Conversion of JSON data to
2with
0
# Conversion of JSON data to
2with
2
# Conversion of JSON data to
2______19[
with
6with
7
# Conversion of JSON data to
2with
9 open
0open
1 open
2# importing the module
2open
4
open
5# Conversion of JSON data to
9[
open
8open
9[
0# importing the module
3
open
5# Conversion of JSON data to
9[
[
5open
9[
7# importing the module
3
open
5# Conversion of JSON data to
9[
# Conversion of JSON data to
02open
9# Conversion of JSON data to
04# importing the module
3
open
5# Conversion of JSON data to
9# Conversion of JSON data to
08
Đầu ra.
Ghi chú cá nhân của tôi arrow_drop_up
Tiết kiệm