Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Function Used:
- Bàn luậnjson.loads[] function is present in python built-in ‘json’ module. This function is used to parse the JSON string.
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 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. Python hỗ trợ JSON thông qua gói tích hợp có tên 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 được trích dẫn có chứa giá trị trong ánh xạ giá trị khóa trong {}. Nó tương tự như từ điển trong python.function được sử dụng: & nbsp; json.load[file_name]
Parameter: It takes JSON file as the parameter.
Return type: It returns the python dictionary object.
json.load []: hàm json.loads [] có mặt trong mô-đun python tích hợp ‘json. Hàm này được sử dụng để phân tích chuỗi JSON. & NBSP; Let’s suppose the JSON file looks like this:
Cú pháp: JSON.LOAD [FILE_NAME] Tham số: Nó lấy tệp JSON làm tham số.return loại: nó trả về đối tượng từ điển Python. & Nbsp; & nbsp;
Python3
Ví dụ 1: Hãy giả sử tệp JSON trông như thế này: & nbsp;
with
open
[
'data.json'
] as json_file:
Chúng tôi muốn chuyển đổi nội dung của tệp này thành từ điển Python. Dưới đây là việc thực hiện. & NBSP;
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject4
[
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject6
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject7
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject8
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject9
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject4
[
import
3import
4import
5import
6
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject4
[
json
0import
4json
2import
6import
json
json.loads[jsonstring] #for Json string
json.loads[fileobject.read[]] #for fileobject
0____11 json.loads[jsonstring] #for Json string
json.loads[fileobject.read[]] #for fileobject
2 Reading nested data
In the above JSON file, there is a nested dictionary in the first key people1. Below is the implementation of reading nested data.
Python3
Ví dụ 1: Hãy giả sử tệp JSON trông như thế này: & nbsp;
with
open
[
'data.json'
] as json_file:
Chúng tôi muốn chuyển đổi nội dung của tệp này thành từ điển Python. Dưới đây là việc thực hiện. & NBSP;
import
json
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject4
[
open
5open
6
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject0____11
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject2
[
4
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject4
[
[
7[
8[
9import
6[
4
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject4
[
'data.json'
4[
8'data.json'
6import
6[
4
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject4
[
] as json_file:
1[
8] as json_file:
3import
6[
4
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject4
] as json_file:
7import
json
‘
json.loads[jsonstring] #for Json string
json.loads[fileobject.read[]] #for fileobject
0____11 json.loads[jsonstring] #for Json string
json.loads[fileobject.read[]] #for fileobject
33
Chúng ta có thể chuyển đổi JSON thành Từ điển trong Python không?
Nếu bạn có chuỗi JSON, bạn có thể phân tích nó bằng cách sử dụng phương thức JSON.LOADS []. Kết quả sẽ là một từ điển Python.
Làm thế nào trích xuất dữ liệu cụ thể từ JSON trong Python?
sự vật | DIGN |
mảng | danh sách |
sợi dây | str |
vô giá trị | Không có |
Số [int] | int |
số [thực] | trôi nổi |
thật | ĐÚNG VẬY |
sai | Sai |
json.load []: json.load [] chấp nhận đối tượng tệp, phân tích dữ liệu JSON, điền từ điển Python với dữ liệu và trả lại cho bạn. & nbsp; json.load[] accepts file object, parses the JSON data, populates a Python dictionary with the data and returns it back to you.
Syntax:
json.load[file object]
Ví dụ: Giả sử tệp JSON trông như thế này: Suppose the JSON file looks like this:
Chúng tôi muốn đọc nội dung của tệp này. Dưới đây là việc thực hiện.
Python3
import
json
‘
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject0____11
8open
8 open
9[
0 [
1
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject03
[
3
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject4
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject07
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject08
Output:
JSON.LOADS []: Nếu bạn có chuỗi JSON, bạn có thể phân tích nó bằng cách sử dụng json.loads [] method.json.loads [] không lấy đường dẫn tệp, mà là nội dung tệp làm chuỗi, sử dụng FileObject. đọc [] với json.loads [] chúng ta có thể trả về nội dung của tệp. & nbsp;
If you have a JSON string, you can parse it by using the json.loads[] method.json.loads[] does not take the file path, but the file contents as a string, using fileobject.read[] with json.loads[] we can return the content of the file.
Syntax:
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject
Ví dụ: Ví dụ này hiển thị đọc từ cả tệp chuỗi và JSON. Các tập tin hiển thị ở trên được sử dụng. This example shows reading from both string and JSON file. The file shown above is used.
Python3
import
json
‘
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject0____11
8json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject4
[
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject19
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject20
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject4
] as json_file:
7open
8 open
9[
0 [
1
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject03
[
3JSON.LOADS []: Nếu bạn có chuỗi JSON, bạn có thể phân tích nó bằng cách sử dụng json.loads [] method.json.loads [] không lấy đường dẫn tệp, mà là nội dung tệp làm chuỗi, sử dụng FileObject. đọc [] với json.loads [] chúng ta có thể trả về nội dung của tệp. & nbsp;
open
8 open
9[
0 [
1
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject03
[
3
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject4
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject07
json.loads[jsonstring] #for Json string json.loads[fileobject.read[]] #for fileobject08
Output: