Python in json

Trong hướng dẫn này, chúng ta sẽ xem cách đọc và ghi dữ liệu JSON từ và tới một tệp trong Python, sử dụng mô-đun

import json

with open('json_data.json') as json_file:
    data = json.load(json_file)
    print(data)
8

JSON (Ký hiệu đối tượng JavaScript) là một định dạng cực kỳ phổ biến để tuần tự hóa dữ liệu, với mức độ nhẹ và dễ áp ​​dụng của nó - đồng thời cũng khá thân thiện với con người. Đáng chú ý nhất, nó được sử dụng rộng rãi trong thế giới phát triển web, nơi bạn có thể gặp các đối tượng tuần tự hóa JSON được gửi từ API REST, cấu hình ứng dụng hoặc thậm chí lưu trữ dữ liệu đơn giản

Với mức độ phổ biến của nó, việc đọc và phân tích cú pháp các tệp JSON (hoặc chuỗi) là khá phổ biến và việc viết JSON để gửi đi cũng phổ biến không kém. Trong hướng dẫn này - chúng ta sẽ xem cách tận dụng mô-đun

import json

with open('json_data.json') as json_file:
    data = json.load(json_file)
    print(data)
8 để đọc và viết JSON trong Python

Viết JSON vào một tệp bằng Python với json. bãi () và json. bãi ()

Để ghi nội dung JSON vào một tệp bằng Python - chúng ta có thể sử dụng

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
0 và
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
1. Đây là những phương pháp riêng biệt và đạt được kết quả khác nhau

  • {'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
    
    1 - Sê-ri hóa một đối tượng thành một chuỗi có định dạng JSON
  • {'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
    
    0 - Đã tuần tự hóa một đối tượng thành luồng JSON để lưu vào tệp hoặc ổ cắm

Python in json

Ghi chú. Chữ "s" trong "dumps" thực ra là viết tắt của "dump string"

Định dạng tự nhiên của JSON tương tự như bản đồ trong khoa học máy tính - bản đồ gồm các cặp

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
4. Trong Python, từ điển là một triển khai bản đồ, vì vậy, đương nhiên chúng ta sẽ có thể biểu diễn JSON một cách trung thực thông qua một
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
5. Một từ điển có thể chứa các từ điển, mảng, booleans lồng nhau khác hoặc các kiểu nguyên thủy khác như số nguyên và chuỗi

:::

Ghi chú. Gói json tích hợp cung cấp một số phương thức tiện lợi cho phép chúng tôi chuyển đổi giữa JSON và từ điển

:::

Như đã nói, hãy nhập mô-đun

import json

with open('json_data.json') as json_file:
    data = json.load(json_file)
    print(data)
8, xác định từ điển với một số dữ liệu và sau đó chuyển đổi nó thành JSON trước khi lưu vào tệp

Kết quả này trong

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}

Ở đây, chúng ta có một từ điển đơn giản với một vài

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
7, mỗi cái có một
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
8,
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
9 và
import json

with open('json_data.json') as json_file:
    data = json.load(json_file)
    print(data)
30. Hàm
import json

with open('json_data.json') as json_file:
    data = json.load(json_file)
    print(data)
31 của mô-đun
import json

with open('json_data.json') as json_file:
    data = json.load(json_file)
    print(data)
8 đưa một từ điển vào nội dung JSON và trả về một chuỗi JSON

Sau khi được tuần tự hóa, bạn có thể quyết định gửi nó đến một dịch vụ khác sẽ giải tuần tự hóa nó, hoặc, ví dụ, lưu trữ nó. Để lưu trữ chuỗi JSON này vào một tệp, chúng tôi chỉ cần mở một tệp ở chế độ ghi và ghi nó xuống. Nếu bạn không muốn trích xuất dữ liệu thành một biến độc lập để sử dụng sau này mà chỉ muốn kết xuất dữ liệu đó vào một tệp, bạn có thể bỏ qua hàm

import json

with open('json_data.json') as json_file:
    data = json.load(json_file)
    print(data)
31 và sử dụng _______134 instad

Bất kỳ đối tượng giống như tệp nào cũng có thể được chuyển đến đối số thứ hai của hàm

import json

with open('json_data.json') as json_file:
    data = json.load(json_file)
    print(data)
34, ngay cả khi đó không phải là tệp thực. Một ví dụ điển hình về điều này sẽ là một ổ cắm, có thể được mở, đóng và ghi vào giống như một tệp

Đọc JSON từ một tệp bằng Python với json. tải () và json. tải ()

Ánh xạ giữa nội dung từ điển và chuỗi JSON rất đơn giản, vì vậy thật dễ dàng để chuyển đổi giữa hai. Logic tương tự như với

import json

with open('json_data.json') as json_file:
    data = json.load(json_file)
    print(data)
34 và
import json

with open('json_data.json') as json_file:
    data = json.load(json_file)
    print(data)
31 được áp dụng cho
import json

with open('json_data.json') as json_file:
    data = json.load(json_file)
    print(data)
38 và
import json

with open('json_data.json') as json_file:
    data = json.load(json_file)
    print(data)
39. Giống như
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
1, hàm
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
1 chấp nhận một chuỗi JSON và chuyển đổi nó thành một từ điển, trong khi
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
2 cho phép bạn tải vào một tệp

import json

with open('json_data.json') as json_file:
    data = json.load(json_file)
    print(data)

Kết quả này trong

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}

Ngoài ra, hãy đọc một chuỗi JSON vào từ điển

import json

with open('json_data.json') as json_file:
    data = json.load(json_file)
    print(data)
3

Điều này cũng dẫn đến

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}

Cái này đặc biệt hữu ích để phân tích các phản hồi API REST gửi JSON. Dữ liệu này đến với bạn dưới dạng một chuỗi, sau đó bạn có thể chuyển trực tiếp đến

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
1 và bạn có một từ điển dễ quản lý hơn nhiều để làm việc với

Sắp xếp, in đẹp, dấu tách và mã hóa

Khi tuần tự hóa dữ liệu của bạn thành JSON bằng Python, định dạng tiêu chuẩn nhằm giảm thiểu bộ nhớ cần thiết để truyền tin nhắn không dễ đọc do khoảng trắng bị loại bỏ. Mặc dù đây là hành vi lý tưởng để truyền dữ liệu (máy tính không quan tâm đến khả năng đọc mà quan tâm đến kích thước) - đôi khi bạn có thể cần thực hiện những thay đổi nhỏ, chẳng hạn như thêm khoảng trắng để con người có thể đọc được

Hãy xem hướng dẫn thực hành, thực tế của chúng tôi để học Git, với các phương pháp hay nhất, tiêu chuẩn được ngành chấp nhận và bao gồm bảng gian lận. Dừng các lệnh Git trên Google và thực sự tìm hiểu nó

Ghi chú.

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
0/
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
1 và
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
2/
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
1 đều cung cấp một số tùy chọn định dạng

JSON in đẹp bằng Python

Làm cho JSON có thể đọc được bằng con người (hay còn gọi là "in đẹp") dễ dàng như chuyển một giá trị số nguyên cho tham số

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
8

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
6

Điều này tạo ra một vết lõm 4 dấu cách trên mỗi khối logic mới

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
7

Một tùy chọn khác là sử dụng công cụ dòng lệnh -

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
9. Với nó, bạn có thể in JSON đẹp mắt trong dòng lệnh mà không ảnh hưởng đến chuỗi được truyền và chỉ ảnh hưởng đến cách nó được hiển thị trên ống đầu ra tiêu chuẩn

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
9

Sắp xếp các đối tượng JSON theo khóa

Theo thuật ngữ JSON

"Một đối tượng là một tập hợp các cặp tên/giá trị không có thứ tự. "

Thứ tự khóa không được đảm bảo, nhưng có thể bạn cần thực thi thứ tự khóa. Để đạt được thứ tự, bạn có thể chuyển

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
60 sang tùy chọn
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
61 khi sử dụng
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
0 hoặc
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
1

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
4

Kết quả này trong

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
5

Văn bản và mã hóa ASCII

Theo mặc định,

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
0 và
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
1 sẽ đảm bảo rằng văn bản trong từ điển Python đã cho được mã hóa ASCII. Nếu có các ký tự không phải ASCII thì chúng sẽ tự động được thoát, như minh họa trong ví dụ sau

import json

with open('json_data.json') as json_file:
    data = json.load(json_file)
    print(data)
0
import json

with open('json_data.json') as json_file:
    data = json.load(json_file)
    print(data)
1

Điều này không phải lúc nào cũng được chấp nhận và trong nhiều trường hợp, bạn có thể muốn giữ nguyên các ký tự Unicode của mình. Để thực hiện việc này, hãy đặt tùy chọn

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
66 thành
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
67

import json

with open('json_data.json') as json_file:
    data = json.load(json_file)
    print(data)
2____03

Bỏ qua các loại dữ liệu khóa tùy chỉnh

Nếu một khóa trong từ điển của bạn thuộc loại không nguyên thủy (

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
68,
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
69,
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
70,
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
71 hoặc
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
72), thì một
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
73 sẽ xuất hiện khi bạn thử kết xuất nội dung JSON vào một tệp. Bạn có thể bỏ qua các khóa này thông qua đối số
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
74

import json

with open('json_data.json') as json_file:
    data = json.load(json_file)
    print(data)
4

Kích hoạt và vô hiệu hóa kiểm tra thông tư

Nếu một thuộc tính của đối tượng JSON tự tham chiếu hoặc một đối tượng khác tham chiếu ngược lại đối tượng gốc - một JSON đệ quy vô hạn được tạo. Đệ quy vô hạn thường dẫn đến việc bộ nhớ được cấp phát nhanh chóng cho đến khi thiết bị hết bộ nhớ và trong trường hợp kết xuất JSON, một

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
75 được nâng lên và quá trình kết xuất bị tạm dừng

Điều này được quy định bởi cờ

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
76, mặc định là
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
60 và ngăn ngừa các sự cố có thể xảy ra khi viết các phụ thuộc vòng tròn. Để tắt nó, bạn có thể đặt nó thành `False

import json

with open('json_data.json') as json_file:
    data = json.load(json_file)
    print(data)
5

Tuy nhiên, xin lưu ý rằng điều này rất không được khuyến khích

Kích hoạt và vô hiệu hóa NaN

Các giá trị NaN, chẳng hạn như

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
78,
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
79 và
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
90 có thể len ​​lỏi vào các đối tượng mà bạn muốn tuần tự hóa hoặc giải tuần tự hóa. Tiêu chuẩn JSON không cho phép các giá trị NaN, nhưng chúng vẫn mang giá trị logic mà bạn có thể muốn truyền tải trong một tin nhắn. Mặt khác - bạn có thể muốn thực thi rằng các giá trị NaN không được truyền đi và thay vào đó đưa ra một ngoại lệ. Cờ
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
91 được đặt thành
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
60 theo mặc định và cho phép bạn tuần tự hóa và giải tuần tự hóa các giá trị NaN, thay thế chúng bằng các giá trị JavaScript tương đương (
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
93,
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
94 và
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
95)

Thay vào đó, nếu bạn đặt cờ thành

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
67 - bạn sẽ chuyển sang định dạng được tiêu chuẩn hóa nghiêm ngặt của JSON, định dạng này sẽ tăng
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
97 nếu đối tượng của bạn chứa các thuộc tính có các giá trị này

import json

with open('json_data.json') as json_file:
    data = json.load(json_file)
    print(data)
6

Thay đổi dấu phân cách

Trong JSON, các khóa được phân tách khỏi các giá trị bằng dấu hai chấm (

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
98) và các mục được phân tách với nhau bằng dấu phẩy (
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
99)

import json

with open('json_data.json') as json_file:
    data = json.load(json_file)
    print(data)
7

Dấu phân cách mặc định để đọc và viết JSON trong Python là

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
40 với khoảng trắng sau dấu phẩy và dấu hai chấm. Bạn có thể thay đổi những điều này để bỏ qua các khoảng trắng và do đó làm cho JSON nhỏ gọn hơn một chút hoặc thay đổi hoàn toàn các dấu phân cách bằng các ký tự đặc biệt khác để thể hiện khác

Các vấn đề về khả năng tương thích với Python 2

Nếu bạn đang sử dụng phiên bản Python cũ hơn (2. x) - bạn có thể gặp phải một

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
73 khi đang cố kết xuất nội dung JSON vào một tệp. Cụ thể, nếu nội dung chứa một ký tự không phải ASCII, thì _____373 sẽ được nâng lên, ngay cả khi bạn chuyển đối số mã hóa, khi sử dụng phương thức ____ ____

Nếu bạn gặp trường hợp cạnh này, trường hợp này đã được sửa trong các phiên bản Python tiếp theo - thay vào đó, hãy thử sử dụng

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
1 và ghi nội dung chuỗi vào tệp thay vì truyền trực tiếp nội dung vào tệp

Sự kết luận

Trong hướng dẫn này, chúng tôi đã giới thiệu cho bạn các phương pháp

{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
0,
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
1,
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
2 và
{'employees': [{'name': 'John Doe', 'department': 'Marketing', 'place': 'Remote'}, {'name': 'Jane Doe', 'department': 'Software Engineering', 'place': 'Remote'}, {'name': 'Don Joe', 'department': 'Software Engineering', 'place': 'Office'}]}
1, giúp tuần tự hóa và giải tuần tự hóa các chuỗi JSON

Sau đó, chúng tôi đã xem xét cách bạn có thể sắp xếp các đối tượng JSON, in đẹp chúng, thay đổi mã hóa, bỏ qua các loại dữ liệu khóa tùy chỉnh, bật hoặc tắt kiểm tra vòng tròn và liệu NaN có được phép hay không, cũng như cách thay đổi dấu phân cách cho

Với việc JSON là một trong những cách phổ biến nhất để tuần tự hóa dữ liệu có cấu trúc, bạn có thể sẽ phải tương tác với nó khá thường xuyên, đặc biệt là khi làm việc trên các ứng dụng web. Mô-đun

import json

with open('json_data.json') as json_file:
    data = json.load(json_file)
    print(data)
8 của Python là một cách tuyệt vời để bắt đầu, mặc dù bạn có thể thấy rằng Simplejson là một giải pháp thay thế tuyệt vời khác ít nghiêm ngặt hơn về cú pháp JSON

Bạn có thể in JSON bằng Python không?

Chúng ta có thể sử dụng mô-đun json của Python để in đẹp dữ liệu JSON . Mô-đun json được khuyến nghị để hoạt động với các tệp JSON. Chúng ta có thể sử dụng phương thức dumps() để lấy chuỗi JSON được định dạng đẹp.

JSON in đẹp là gì?

In đẹp là một hình thức định dạng phong cách bao gồm thụt đầu dòng và tô màu . JSON (Ký hiệu đối tượng JavaScript) là một định dạng trao đổi dữ liệu nhẹ. Con người dễ dàng đọc và viết và máy móc có thể phân tích và tạo ra. Loại phương tiện Internet chính thức cho JSON là ứng dụng/json.