Chuỗi python thành mảng json

Để chuyển đổi Chuỗi JSON thành Danh sách Python, hãy sử dụng json. chức năng tải []. hàm loading[] lấy chuỗi JSON Array làm đối số và trả về Danh sách Python

cú pháp

Cú pháp sử dụng json. phương thức tải [] là

import json

aList = json.dumps[jsonString]

Chúng ta phải nhập gói json để sử dụng json. hàm bãi []

Ghi chú. Xin lưu ý rằng hàm dumps[] trả về Danh sách Python, chỉ khi chuỗi JSON là Mảng JSON

ví dụ 1. Chuyển đổi chuỗi mảng JSON thành danh sách Python

Trong ví dụ này, chúng tôi sẽ lấy một chuỗi JSON Array và chuyển đổi nó thành Danh sách Python. Mảng JSON có hai phần tử, với mỗi phần tử chứa hai khóa. cặp giá trị của mỗi

Sau khi tải chuỗi JSON vào danh sách, chúng ta sẽ in giá trị cho khóa “b”

Chương trình Python

import json

jsonStr = '[{"a":1, "b":2}, {"c":3, "d":4}]'
aList = json.loads[jsonStr]
print[aList[0]['b']]
Chạy

đầu ra

2

ví dụ 2. Chuyển đổi mảng JSON của chuỗi mảng thành danh sách Python

Trong ví dụ này, chúng tôi sẽ lấy một Chuỗi JSON với Mảng của Mảng và chuyển đổi nó thành Danh sách Danh sách của Python

Sau khi phân tích cú pháp chuỗi JSON thành Danh sách Python, chúng ta sẽ truy cập phần tử đầu tiên của phần đầu tiên trong toàn bộ danh sách

Để chuyển đổi Danh sách Python thành JSON, hãy sử dụng json. hàm bãi []. Hàm dumps[] lấy danh sách làm đối số và trả về Chuỗi JSON

cú pháp

Cú pháp sử dụng json. phương thức dumps[] là

import json

jsonString = json.dumps[list]

Chúng ta phải nhập gói json để sử dụng json. bãi []

ví dụ 1. Chuyển đổi danh sách Python thành JSON

Trong ví dụ này, chúng tôi sẽ lấy Danh sách Python với Từ điển làm phần tử và chuyển đổi nó thành chuỗi JSON

Gửi và nhận dữ liệu thường ở dạng chuỗi từ điển [đối tượng JSON] trong nhiều API web để sử dụng dữ liệu đó để trích xuất thông tin có ý nghĩa, chúng tôi cần chuyển đổi dữ liệu đó ở dạng từ điển và sử dụng cho các hoạt động tiếp theo

Hãy xem một số cách để chuyển đổi chuỗi thành json.

Phương pháp #1. đối tượng dict thành đối tượng chuỗi bằng cách sử dụng

import json

jsonStr = '[{"a":1, "b":2}, {"c":3, "d":4}]'
aList = json.loads[jsonStr]
print[aList[0]['b']]
4




import json

jsonStr = '[{"a":1, "b":2}, {"c":3, "d":4}]'
aList = json.loads[jsonStr]
print[aList[0]['b']]
5

import json

jsonStr = '[{"a":1, "b":2}, {"c":3, "d":4}]'
aList = json.loads[jsonStr]
print[aList[0]['b']]
6

import json

jsonStr = '[{"a":1, "b":2}, {"c":3, "d":4}]'
aList = json.loads[jsonStr]
print[aList[0]['b']]
7

import json

jsonStr = '[{"a":1, "b":2}, {"c":3, "d":4}]'
aList = json.loads[jsonStr]
print[aList[0]['b']]
8
import json

jsonStr = '[{"a":1, "b":2}, {"c":3, "d":4}]'
aList = json.loads[jsonStr]
print[aList[0]['b']]
9

2
0

2
1

initial 1st dictionary {'nikhil': 1, 'akash' : 5, 'manjeet' : 10, 'akshat' : 15}
type of ini_object 
final dictionary {'nikhil': 1, 'manjeet': 10, 'akash': 5, 'akshat': 15}
type of final_dictionary 
0
initial 1st dictionary {'nikhil': 1, 'akash' : 5, 'manjeet' : 10, 'akshat' : 15}
type of ini_object 
final dictionary {'nikhil': 1, 'manjeet': 10, 'akash': 5, 'akshat': 15}
type of final_dictionary 
1
initial 1st dictionary {'nikhil': 1, 'akash' : 5, 'manjeet' : 10, 'akshat' : 15}
type of ini_object 
final dictionary {'nikhil': 1, 'manjeet': 10, 'akash': 5, 'akshat': 15}
type of final_dictionary 
2
initial 1st dictionary {'nikhil': 1, 'akash' : 5, 'manjeet' : 10, 'akshat' : 15}
type of ini_object 
final dictionary {'nikhil': 1, 'manjeet': 10, 'akash': 5, 'akshat': 15}
type of final_dictionary 
3
initial 1st dictionary {'nikhil': 1, 'akash' : 5, 'manjeet' : 10, 'akshat' : 15}
type of ini_object 
final dictionary {'nikhil': 1, 'manjeet': 10, 'akash': 5, 'akshat': 15}
type of final_dictionary 
4
initial 1st dictionary {'nikhil': 1, 'akash' : 5, 'manjeet' : 10, 'akshat' : 15}
type of ini_object 
final dictionary {'nikhil': 1, 'manjeet': 10, 'akash': 5, 'akshat': 15}
type of final_dictionary 
5
initial 1st dictionary {'nikhil': 1, 'akash' : 5, 'manjeet' : 10, 'akshat' : 15}
type of ini_object 
final dictionary {'nikhil': 1, 'manjeet': 10, 'akash': 5, 'akshat': 15}
type of final_dictionary 
6
initial 1st dictionary {'nikhil': 1, 'akash' : 5, 'manjeet' : 10, 'akshat' : 15}
type of ini_object 
final dictionary {'nikhil': 1, 'manjeet': 10, 'akash': 5, 'akshat': 15}
type of final_dictionary 
7
initial 1st dictionary {'nikhil': 1, 'akash' : 5, 'manjeet' : 10, 'akshat' : 15}
type of ini_object 
final dictionary {'nikhil': 1, 'manjeet': 10, 'akash': 5, 'akshat': 15}
type of final_dictionary 
4
initial 1st dictionary {'nikhil': 1, 'akash' : 5, 'manjeet' : 10, 'akshat' : 15}
type of ini_object 
final dictionary {'nikhil': 1, 'manjeet': 10, 'akash': 5, 'akshat': 15}
type of final_dictionary 
9
import json

jsonStr = '[{"a":1, "b":2}, {"c":3, "d":4}]'
aList = json.loads[jsonStr]
print[aList[0]['b']]
40

Chủ Đề