Tôi đang cố gắng tạo một kết quả truy vấn API trong JSON và lưu trữ giá trị vào danh sách.
import requests
url = "//api_url/json/"
headers = {
'auth': "0dXKLHO",
}
response = requests.request['GET', url, headers=headers, verify=False]
json_data = response.json[]
details = []
for data in json_data["data"]["segment"]:
output = [data['value']]
details.append[output]
print[details]
JSON thực tế là,
{
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
Mã của tôi cung cấp cho tôi đầu ra sau,
['103']
['103','104']
['103','104','105']
Nhưng đầu ra dự kiến nằm trong danh sách đơn,
['103','104','105']
Một số chương trình có thể yêu cầu các loại dữ liệu khác nhau được người dùng chấp nhận. Dù mục đích của chương trình là gì, bạn sẽ cần các cấu trúc dữ liệu như danh sách và từ điển để lưu trữ chúng. Bạn sẽ luôn muốn lưu dữ liệu mà người dùng nhập trước khi họ đóng chương trình của bạn. Cách đơn giản nhất để làm điều này là sử dụng mô -đun JSON để lưu trữ dữ liệu của bạn.
Trong hướng dẫn này, chúng tôi sẽ xem xét cách lưu trữ dữ liệu trong Python bằng mô -đun JSON. Chúng ta cũng sẽ học cách sử dụng các phương pháp
{
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
6 và {
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
7, các phương pháp {
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
8 và {
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
9 và sự khác biệt của chúng. Cuối cùng, chúng ta sẽ xem xét làm thế nào để tuần tự hóa và hạ bệ JSON để phản đối trong Python.Điều kiện tiên quyết
Có một số kiến thức cơ bản về ngôn ngữ lập trình Python.
Tại sao lưu trữ dữ liệu trong Python bằng mô -đun JSON?
- Cho phép các nhà phát triển đổ các cấu trúc dữ liệu đơn giản vào một tệp và tải chúng khi cần
- Dữ liệu có thể được chia sẻ giữa các chương trình Python bằng JSON
- Định dạng JSON là nền tảng hoặc độc lập ngôn ngữ. Khi bạn lưu trữ dữ liệu ở định dạng JSON, bạn cũng có thể sử dụng chúng trong các ngôn ngữ lập trình khác
- Nó rất đơn giản để học và có định dạng di động
Sử dụng json.dump []
Để sử dụng chức năng
{
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
6 Nhập mô -đun ['103']
['103','104']
['103','104','105']
1 trước. Để nhập mô -đun ['103']
['103','104']
['103','104','105']
1, sử dụng ['103']
['103','104']
['103','104','105']
3. {
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
6 giúp ghi dữ liệu vào tệp JSON.Syntax:
Hàm
{
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
6 có hai đối số:- Dữ liệu cần được ghi vào tệp JSON.
- Một đối tượng tệp có thể được sử dụng để lưu dữ liệu
Hãy để phát triển một chương trình nhanh chóng để lưu một bộ số trong tệp JSON. Để lưu tập hợp số, chúng tôi sẽ sử dụng hàm
{
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
6:import json
numbers = [10, 20, 30, 70, 191, 23] #create a set of numbers
filename = 'numbers.json' #use the file extension .json
with open[filename, 'w'] as file_object: #open the file in write mode
json.dump[numbers, file_object] # json.dump[] function to stores the set of numbers in numbers.json file
Trong chương trình này, chúng tôi lưu trữ tập hợp các số trong
['103']
['103','104']
['103','104','105']
7. Phần mở rộng ['103']
['103','104']
['103','104','105']
8 cho thấy tệp chứa dữ liệu ở định dạng JSON.Sau đó, chúng tôi truy cập tệp ở chế độ
['103']
['103','104']
['103','104','105']
9 [chế độ ghi], để tạo dữ liệu được ghi vào tệp JSON. Cuối cùng, hàm {
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
6 lưu trữ tập hợp các số trong tệp ['103']
['103','104']
['103','104','105']
7.Chương trình này không có đầu ra đầu cuối, nhưng khi chúng tôi mở tệp
['103']
['103','104']
['103','104','105']
7, chúng tôi sẽ thấy dữ liệu sau:[10, 20, 30, 70, 191, 23]
Sử dụng json.dumps []
{
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
7 có thể được sử dụng để chuyển đổi đối tượng Python thành chuỗi JSON.Syntax:
Hàm
{
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
7 có một tham số, là dữ liệu được chuyển đổi thành chuỗi JSON.Hãy để một cái nhìn vào ví dụ dưới đây:
import json
data = {
'Name' : 'Felix',
'Occupation' : 'Doctor'
}
dict_1 = json.dumps[data] # converting dictionary to JSON
print[dict_1] # {'Name' : 'Felix','Occupation' : 'Doctor'}
Sự khác biệt giữa json.dumps [] và json.dump []
- Phương thức
5 lấy hai tham số [dữ liệu và tệp], trong khi phương thức['103','104','105']
6 chỉ lấy một tham số [dữ liệu]['103','104','105']
- Phương thức
5 được kết hợp với các hoạt động tệp, không giống như phương thức['103','104','105']
6.['103','104','105']
Sử dụng json.load []
Chúng tôi sử dụng chức năng
['103','104','105']
9 để đọc tệp JSON.Hàm
{
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
8 có một đối số là đối tượng tệp.Syntax:
Giả sử, chúng ta có một tệp JSON có tên
import json
numbers = [10, 20, 30, 70, 191, 23] #create a set of numbers
filename = 'numbers.json' #use the file extension .json
with open[filename, 'w'] as file_object: #open the file in write mode
json.dump[numbers, file_object] # json.dump[] function to stores the set of numbers in numbers.json file
1, chứa các đối tượng JSON.{
"name": "Felix",
"Subjects": ["English", "Political Science"]
}
Hãy để viết một mã để đọc dữ liệu được lưu trữ trong tệp
import json
numbers = [10, 20, 30, 70, 191, 23] #create a set of numbers
filename = 'numbers.json' #use the file extension .json
with open[filename, 'w'] as file_object: #open the file in write mode
json.dump[numbers, file_object] # json.dump[] function to stores the set of numbers in numbers.json file
1 bằng hàm ['103','104','105']
9.import json
with open[r,'student.json'] as file_object:
data = json.load[file_object]
print[data] # {"name": "Felix", "Subjects": ["English", "Political Science"]}
Hàm
{
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
8 phân tích tệp JSON và trả về một từ điển có tên import json
numbers = [10, 20, 30, 70, 191, 23] #create a set of numbers
filename = 'numbers.json' #use the file extension .json
with open[filename, 'w'] as file_object: #open the file in write mode
json.dump[numbers, file_object] # json.dump[] function to stores the set of numbers in numbers.json file
5.Sử dụng json.loads []
Chúng tôi sử dụng phương thức
{
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
9 để phân tích chuỗi JSON và trả về một đối tượng Python như từ điển. Phương thức {
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
9 lấy nội dung tệp làm chuỗi.Syntax:
Example:
import json
# JSON string:
dict_1 = {
"Name": "Felix Maina",
"Contact Number": 0712345678,
"Email": "",
}
# parse dict_1:
y = json.loads[dict_1]
# the result is a Python dictionary:
print[y] #{ "Name": "Felix Maina", "Contact Number": 0712345678,"Email": "", }
Ở đây, chuỗi
import json
numbers = [10, 20, 30, 70, 191, 23] #create a set of numbers
filename = 'numbers.json' #use the file extension .json
with open[filename, 'w'] as file_object: #open the file in write mode
json.dump[numbers, file_object] # json.dump[] function to stores the set of numbers in numbers.json file
8 được phân tích cú pháp bằng phương pháp {
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
9 trả về từ điển có tên [10, 20, 30, 70, 191, 23]
0.Lưu ý: Sự khác biệt chính giữa
9 và{ "data": { "segments": [ { "id": "1", "event_id": "1", "object_id": "0", "object_relation": null "value": "103" }, { "id": "2", "event_id": "1", "object_id": "0", "object_relation": null "value": "104" }, { "id": "3", "event_id": "1", "object_id": "0", "object_relation": null "value": "105" } ] } }
8 là{ "data": { "segments": [ { "id": "1", "event_id": "1", "object_id": "0", "object_relation": null "value": "103" }, { "id": "2", "event_id": "1", "object_id": "0", "object_relation": null "value": "104" }, { "id": "3", "event_id": "1", "object_id": "0", "object_relation": null "value": "105" } ] } }
9 đọc các chuỗi trong khi{ "data": { "segments": [ { "id": "1", "event_id": "1", "object_id": "0", "object_relation": null "value": "103" }, { "id": "2", "event_id": "1", "object_id": "0", "object_relation": null "value": "104" }, { "id": "3", "event_id": "1", "object_id": "0", "object_relation": null "value": "105" } ] } }
8 được sử dụng để đọc các tệp.{ "data": { "segments": [ { "id": "1", "event_id": "1", "object_id": "0", "object_relation": null "value": "103" }, { "id": "2", "event_id": "1", "object_id": "0", "object_relation": null "value": "104" }, { "id": "3", "event_id": "1", "object_id": "0", "object_relation": null "value": "105" } ] } }
Tuần tự hóa dữ liệu JSON trong Python
Tuần tự hóa là quá trình chuyển đổi một loại dữ liệu gốc sang định dạng JSON.
Mô -đun
[10, 20, 30, 70, 191, 23]
5 chuyển đổi một đối tượng từ điển Python thành một đối tượng JSON. Các phương pháp {
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
6 và {
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
7 được sử dụng để tuần tự hóa dữ liệu Python thành định dạng JSON.Hãy cùng xem một ví dụ bằng phương pháp
{
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
6:{
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
0Tại đây, chúng tôi chuyển đổi một từ điển Python thành tệp được định dạng JSON có tên
[10, 20, 30, 70, 191, 23]
9.Phương thức
{
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
7 chuyển đổi một đối tượng Python thành chuỗi JSON như được minh họa dưới đây:{
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
1Deserialize JSON để phản đối trong Python
Hủy bỏ là quá trình chuyển đổi dữ liệu JSON thành một loại dữ liệu gốc. Tại đây, chúng tôi chuyển đổi dữ liệu JSON trở lại từ điển trong Python.
Chúng tôi sử dụng phương thức
{
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
9 để giảm dữ liệu JSON vào đối tượng Python. Phương pháp {
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
8 cũng được sử dụng để giảm dần tệp được định dạng JSON vào một đối tượng Python.Ví dụ: Hủy bỏ bằng cách sử dụng
3 # Nhập mô -đun: Deserialization using the import json
data = {
'Name' : 'Felix',
'Occupation' : 'Doctor'
}
dict_1 = json.dumps[data] # converting dictionary to JSON
print[dict_1] # {'Name' : 'Felix','Occupation' : 'Doctor'}
import json
data = {
'Name' : 'Felix',
'Occupation' : 'Doctor'
}
dict_1 = json.dumps[data] # converting dictionary to JSON
print[dict_1] # {'Name' : 'Felix','Occupation' : 'Doctor'}
3 # importing the module{
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
2Output::
{
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
3Hãy để tạo một tệp và đặt tên cho nó
import json
data = {
'Name' : 'Felix',
'Occupation' : 'Doctor'
}
dict_1 = json.dumps[data] # converting dictionary to JSON
print[dict_1] # {'Name' : 'Felix','Occupation' : 'Doctor'}
4. Tệp này sẽ có dữ liệu sau:{
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
4Bây giờ, hãy để Deserialize hóa tệp này bằng hàm
import json
data = {
'Name' : 'Felix',
'Occupation' : 'Doctor'
}
dict_1 = json.dumps[data] # converting dictionary to JSON
print[dict_1] # {'Name' : 'Felix','Occupation' : 'Doctor'}
5:{
"data": {
"segments": [
{
"id": "1",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "103"
},
{
"id": "2",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "104"
},
{
"id": "3",
"event_id": "1",
"object_id": "0",
"object_relation": null
"value": "105"
}
]
}
}
5Sự kết luận
Trong bài viết này, chúng tôi đã học được như sau:
- Lý do lưu trữ dữ liệu trong Python bằng mô -đun JSON
- Sử dụng
6,{ "data": { "segments": [ { "id": "1", "event_id": "1", "object_id": "0", "object_relation": null "value": "103" }, { "id": "2", "event_id": "1", "object_id": "0", "object_relation": null "value": "104" }, { "id": "3", "event_id": "1", "object_id": "0", "object_relation": null "value": "105" } ] } }
7 và sự khác biệt của chúng{ "data": { "segments": [ { "id": "1", "event_id": "1", "object_id": "0", "object_relation": null "value": "103" }, { "id": "2", "event_id": "1", "object_id": "0", "object_relation": null "value": "104" }, { "id": "3", "event_id": "1", "object_id": "0", "object_relation": null "value": "105" } ] } }
- Sử dụng
8,{ "data": { "segments": [ { "id": "1", "event_id": "1", "object_id": "0", "object_relation": null "value": "103" }, { "id": "2", "event_id": "1", "object_id": "0", "object_relation": null "value": "104" }, { "id": "3", "event_id": "1", "object_id": "0", "object_relation": null "value": "105" } ] } }
9 và sự khác biệt của chúng{ "data": { "segments": [ { "id": "1", "event_id": "1", "object_id": "0", "object_relation": null "value": "103" }, { "id": "2", "event_id": "1", "object_id": "0", "object_relation": null "value": "104" }, { "id": "3", "event_id": "1", "object_id": "0", "object_relation": null "value": "105" } ] } }
- Tuần tự hóa và giải phóng dữ liệu JSON trong Python
đọc thêm
Để biết thêm thông tin về mô -đun JSON trong Python, hãy xem các liên kết dưới đây:
- Mô -đun Python JSON
- Giới thiệu về Mô -đun JSON trong Python
Đóng góp đánh giá ngang hàng của: Srishilesh P S