Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 11 million other learners and get started learning Python for data science today!
Good news! You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount
การทำให้เป็นอนุกรม
Python มีไลบรารี JSON ที่สร้างขึ้นมาในตัวเพื่อการเข้ารหัสและถอดรหัส JSON
ใน Python 2.5 โมดูล simplejson ถูกใช้งาน, แต่ใน Python 2.7 โมดูล json ถูกใช้งาน. เนื่องจาก interpreter นี้ใช้ Python 2.7, เราจะใช้งาน json
เพื่อที่จะใช้งานโมดูล json, ก่อนอื่นจะต้อง import:
import json
มีสองรูปแบบพื้นฐานสำหรับข้อมูล JSON. ไม่ว่าจะเป็นในรูปแบบสตริงหรือโครงสร้างข้อมูลอ็อบเจ็กต์. โครงสร้างข้อมูลอ็อบเจ็กต์, ใน Python, ประกอบด้วย lists และ dictionaries ที่ซ้อนกัน. โครงสร้างข้อมูลอ็อบเจ็กต์อนุญาตให้ใช้ methods ของ python (สำหรับ lists และ dictionaries) ในการเพิ่ม, ลิสต์, ค้นหา และลบองค์ประกอบจากโครงสร้างข้อมูล. รูปแบบสตริงมักจะใช้ในการส่งข้อมูลไปยังโปรแกรมอื่นหรือโหลดเข้าโครงสร้างข้อมูล.
ในการโหลด JSON กลับสู่โครงสร้างข้อมูล, ใช้เมธอด "loads". เมธอดนี้รับสตริงและเปลี่ยนให้กลับเป็นโครงสร้างข้อมูลอ็อบเจ็กต์ json:
import json
print(json.loads(json_string))
ในการเข้ารหัสโครงสร้างข้อมูลเป็น JSON, ใช้เมธอด "dumps". เมธอดนี้รับอ็อบเจ็กต์และคืนค่าเป็นสตริง:
import json
json_string = json.dumps([1, 2, 3, "a", "b", "c"])
print(json_string)
Python สนับสนุนวิธีการทำซีเรียลไลส์ข้อมูลที่เป็นกรรมสิทธิ์ของ Python ชื่อว่า pickle (และมีทางเลือกที่เร็วกว่าเรียกว่า cPickle).
คุณสามารถใช้งานมันได้ในลักษณะเดียวกัน
import pickle
pickled_string = pickle.dumps([1, 2, 3, "a", "b", "c"])
print(pickle.loads(pickled_string))
Exercise--------
เป้าหมายของแบบฝึกหัดนี้คือการพิมพ์สตริง JSON ที่มีคู่คีย์-ค่าที่ว่า "Me" : 800 ถูกเพิ่มเข้าไปในนั้น.
import json
# fix this function, so it adds the given name
# and salary pair to salaries_json, and return it
def add_employee(salaries_json, name, salary):
# Add your code here
return salaries_json
# test code
salaries = '{"Alfred" : 300, "Jane" : 400 }'
new_salaries = add_employee(salaries, "Me", 800)
decoded_salaries = json.loads(new_salaries)
print(decoded_salaries["Alfred"])
print(decoded_salaries["Jane"])
print(decoded_salaries["Me"])
import json
# fix this function, so it adds the given name
# and salary pair to salaries_json, and return it
def add_employee(salaries_json, name, salary):
salaries = json.loads(salaries_json)
salaries[name] = salary
return json.dumps(salaries)
# test code
salaries = '{"Alfred" : 300, "Jane" : 400 }'
new_salaries = add_employee(salaries, "Me", 800)
decoded_salaries = json.loads(new_salaries)
print(decoded_salaries["Alfred"])
print(decoded_salaries["Jane"])
print(decoded_salaries["Me"])
test_output_contains("300")
test_output_contains("400")
test_output_contains("800")
success_msg("Great work!")
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!