Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีแปลง JSON Data เป็น Python Object

โค้ดต่อไปนี้แปลง JSON Object (String) เป็น Python Object (Dictionary) เรา Import โมดูล json และใช้เมธอด json.loads() เพื่อทำสิ่งนี้

ตัวอย่าง

import json

json_string = '{"name":"Sonali", "age": 21, "designation":"Software developer"}'
print(type(json_string))

def func(strng):
    a = json.loads(strng)
    print(type(a))
    print(a)

func(json_string)

ผลลัพธ์

<class 'str'>
<class 'dict'>
{'age': 21, 'name': 'Sonali', 'designation': 'Software developer'}