JSON là viết tắt của JavaScript Object Notation. Đó là một cú pháp đơn giản để lưu trữ dữ liệu theo cặp tên-giá trị. Các giá trị có thể là các loại dữ liệu khác nhau miễn là chúng hợp lệ. Các loại không được chấp nhận cho JSON bao gồm hàm, ngày tháng và undefined
Các tệp JSON được lưu trữ với phần mở rộng .json
với cấu trúc JSON hợp lệ
Đây là cấu trúc của một tệp JSON trông như thế nào
{
"name": "John",
"age": 50,
"is_married": false,
"profession": null,
"hobbies": ["traveling", "photography"]
}
Bạn sẽ thường sử dụng JSON để gửi và nhận dữ liệu từ máy chủ trong các ứng dụng web
Khi nhận được dữ liệu, chương trình sẽ đọc và phân tích cú pháp JSON để trích xuất dữ liệu cụ thể. Các ngôn ngữ khác nhau có phương pháp riêng để thực hiện việc này. Chúng ta sẽ xem cách thực hiện những điều này trong Python tại đây
Cách đọc tệp JSON
Giả sử JSON trong khối mã ở trên được lưu trữ trong tệp user.json
. Sử dụng hàm có sẵn open[]
trong Python, chúng ta có thể đọc tệp đó và gán nội dung cho một biến. Đây là cách
with open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
# {
# "name": "John",
# "age": 50,
# "is_married": false,
# "profession": null,
# "hobbies": ["travelling", "photography"]
# }
Bạn chuyển đường dẫn tệp đến phương thức open
để mở tệp và gán dữ liệu luồng từ tệp cho biến user_file
. Sử dụng phương thức
with open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
# {
# "name": "John",
# "age": 50,
# "is_married": false,
# "profession": null,
# "hobbies": ["travelling", "photography"]
# }
0, bạn có thể chuyển nội dung văn bản của tệp vào biến with open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
# {
# "name": "John",
# "age": 50,
# "is_married": false,
# "profession": null,
# "hobbies": ["travelling", "photography"]
# }
1Tôi đã sử dụng
with open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
# {
# "name": "John",
# "age": 50,
# "is_married": false,
# "profession": null,
# "hobbies": ["travelling", "photography"]
# }
2 ở đầu biểu thức để sau khi đọc nội dung của tệp, Python có thể đóng tệpwith open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
# {
# "name": "John",
# "age": 50,
# "is_married": false,
# "profession": null,
# "hobbies": ["travelling", "photography"]
# }
1 hiện chứa phiên bản chuỗi hóa của JSON. Bước tiếp theo, bây giờ bạn có thể phân tích cú pháp JSONCách phân tích cú pháp JSON
Python có các mô-đun dựng sẵn cho các hoạt động khác nhau. Để quản lý tệp JSON, Python có mô-đun
with open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
# {
# "name": "John",
# "age": 50,
# "is_married": false,
# "profession": null,
# "hobbies": ["travelling", "photography"]
# }
4Mô-đun này đi kèm với nhiều phương pháp. Một trong số đó là phương pháp
with open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
# {
# "name": "John",
# "age": 50,
# "is_married": false,
# "profession": null,
# "hobbies": ["travelling", "photography"]
# }
5 để phân tích chuỗi JSON. Sau đó, bạn có thể gán dữ liệu được phân tích cú pháp cho một biến như thế này________số 8_______Sử dụng phương pháp
with open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
# {
# "name": "John",
# "age": 50,
# "is_married": false,
# "profession": null,
# "hobbies": ["travelling", "photography"]
# }
5, bạn có thể thấy rằng biến with open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
# {
# "name": "John",
# "age": 50,
# "is_married": false,
# "profession": null,
# "hobbies": ["travelling", "photography"]
# }
7 hiện có một từ điển hợp lệ. Từ từ điển này, bạn có thể truy cập các khóa và giá trị trong đóCũng lưu ý cách
with open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
# {
# "name": "John",
# "age": 50,
# "is_married": false,
# "profession": null,
# "hobbies": ["travelling", "photography"]
# }
8 từ JSON được chuyển đổi thành with open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
# {
# "name": "John",
# "age": 50,
# "is_married": false,
# "profession": null,
# "hobbies": ["travelling", "photography"]
# }
9 trong python. Điều này là do with open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
# {
# "name": "John",
# "age": 50,
# "is_married": false,
# "profession": null,
# "hobbies": ["travelling", "photography"]
# }
8 không hợp lệ trong import json
with open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
parsed_json = json.loads[file_contents]
# {
# 'name': 'John',
# 'age': 50,
# 'is_married': False,
# 'profession': None,
# 'hobbies': ['travelling', 'photography']
# }
1Cách sử dụng import json
with open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
parsed_json = json.loads[file_contents]
# {
# 'name': 'John',
# 'age': 50,
# 'is_married': False,
# 'profession': None,
# 'hobbies': ['travelling', 'photography']
# }
2 để đọc và phân tích tệp JSON
import json
with open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
parsed_json = json.loads[file_contents]
# {
# 'name': 'John',
# 'age': 50,
# 'is_married': False,
# 'profession': None,
# 'hobbies': ['travelling', 'photography']
# }
Mô-đun
with open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
# {
# "name": "John",
# "age": 50,
# "is_married": false,
# "profession": null,
# "hobbies": ["travelling", "photography"]
# }
4 cũng có phương thức import json
with open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
parsed_json = json.loads[file_contents]
# {
# 'name': 'John',
# 'age': 50,
# 'is_married': False,
# 'profession': None,
# 'hobbies': ['travelling', 'photography']
# }
4 mà bạn có thể sử dụng để đọc một đối tượng tệp và phân tích cú pháp nó cùng một lúc. Sử dụng phương pháp này, bạn có thể cập nhật mã trước đó thành mã nàyimport json
with open['user.json'] as user_file:
parsed_json = json.load[user_file]
print[parsed_json]
# {
# 'name': 'John',
# 'age': 50,
# 'is_married': False,
# 'profession': None,
# 'hobbies': ['travelling', 'photography']
# }
Thay vì sử dụng phương thức
with open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
# {
# "name": "John",
# "age": 50,
# "is_married": false,
# "profession": null,
# "hobbies": ["travelling", "photography"]
# }
0 của đối tượng tệp và sử dụng phương thức import json
with open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
parsed_json = json.loads[file_contents]
# {
# 'name': 'John',
# 'age': 50,
# 'is_married': False,
# 'profession': None,
# 'hobbies': ['travelling', 'photography']
# }
6 của mô-đun with open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
# {
# "name": "John",
# "age": 50,
# "is_married": false,
# "profession": null,
# "hobbies": ["travelling", "photography"]
# }
4, bạn có thể trực tiếp sử dụng phương thức import json
with open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
parsed_json = json.loads[file_contents]
# {
# 'name': 'John',
# 'age': 50,
# 'is_married': False,
# 'profession': None,
# 'hobbies': ['travelling', 'photography']
# }
4 để đọc và phân tích cú pháp đối tượng tệpkết thúc
Dữ liệu JSON thường được biết đến với cấu trúc đơn giản và phổ biến [là tiêu chuẩn trong hầu hết các trường hợp] để trao đổi thông tin giữa máy chủ và máy khách
Các ngôn ngữ và công nghệ khác nhau có thể đọc và phân tích các tệp JSON theo những cách khác nhau. Trong bài viết này, chúng ta đã học cách đọc các tệp JSON và phân tích cú pháp các tệp đó bằng cách sử dụng phương thức
with open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
# {
# "name": "John",
# "age": 50,
# "is_married": false,
# "profession": null,
# "hobbies": ["travelling", "photography"]
# }
0 của các đối tượng tệp và các phương thức import json
with open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
parsed_json = json.loads[file_contents]
# {
# 'name': 'John',
# 'age': 50,
# 'is_married': False,
# 'profession': None,
# 'hobbies': ['travelling', 'photography']
# }
6 và import json
with open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
parsed_json = json.loads[file_contents]
# {
# 'name': 'John',
# 'age': 50,
# 'is_married': False,
# 'profession': None,
# 'hobbies': ['travelling', 'photography']
# }
4 của mô-đun with open['user.json'] as user_file:
file_contents = user_file.read[]
print[file_contents]
# {
# "name": "John",
# "age": 50,
# "is_married": false,
# "profession": null,
# "hobbies": ["travelling", "photography"]
# }
4QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
Người ủng hộ nhà phát triển và Người sáng tạo nội dung đam mê chia sẻ kiến thức của tôi về Công nghệ. Tôi đơn giản hóa JavaScript / ReactJS / NodeJS / Frameworks / TypeScript / v.v. Kênh YT của tôi. youtube. com/c/deeecode
Nếu bạn đọc đến đây, hãy tweet cho tác giả để cho họ thấy bạn quan tâm. Tweet một lời cảm ơn
Học cách viết mã miễn phí. Chương trình giảng dạy mã nguồn mở của freeCodeCamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu