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 provides built-in JSON libraries to encode and decode JSON.

ב-Python 2.5, משתמשים במודול simplejson, ואילו ב-Python 2.7, משתמשים במודול json. מכיוון שמפרש זה משתמש ב-Python 2.7, נשתמש ב-json.

על מנת להשתמש במודול json, יש לייבא אותו תחילה:

import json

יש שני פורמטים בסיסיים לנתוני JSON. או במחרוזת או במבנה נתונים של אובייקט. מבנה הנתונים של אובייקט, ב-Python, מורכב מרשימות ומילונים מקוננים זה בזה. מבנה הנתונים של האובייקט מאפשר להשתמש בשיטות פייתון (לרשימות ומילונים) להוספה, רישום, חיפוש והסרת פריטים ממבנה הנתונים. פורמט המחרוזת משמש בעיקר להעברת הנתונים לתוכנית אחרת או לטעינתם למבנה נתונים.

כדי לטעון את ה-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 supports a Python proprietary data serialization method called pickle (and a faster alternative called cPickle).

You can use it exactly the same way.

import pickle
pickled_string = pickle.dumps([1, 2, 3, "a", "b", "c"])
print(pickle.loads(pickled_string))

תרגיל

מטרת התרגיל היא להדפיס את מחרוזת ה-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!

Previous Tutorial Next Tutorial Take the Test
Copyright © learnpython.org. Read our Terms of Use and Privacy Policy