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, יש לייבא אותו תחילה:
יש שני פורמטים בסיסיים לנתוני JSON. או במחרוזת או במבנה נתונים של אובייקט. מבנה הנתונים של אובייקט, ב-Python, מורכב מרשימות ומילונים מקוננים זה בזה. מבנה הנתונים של האובייקט מאפשר להשתמש בשיטות פייתון (לרשימות ומילונים) להוספה, רישום, חיפוש והסרת פריטים ממבנה הנתונים. פורמט המחרוזת משמש בעיקר להעברת הנתונים לתוכנית אחרת או לטעינתם למבנה נתונים.
כדי לטעון את ה-JSON חזרה למבנה נתונים, יש להשתמש בשיטת "loads". שיטה זו לוקחת מחרוזת ומחזירה אותה למבנה נתונים של אובייקט JSON:
כדי לקודד את מבנה הנתונים ל-JSON, השתמשו בשיטת "dumps". שיטה זו לוקחת אובייקט ומחזירה מחרוזת:
Python supports a Python proprietary data serialization method called pickle (and a faster alternative called cPickle).
You can use it exactly the same way.
תרגיל
מטרת התרגיל היא להדפיס את מחרוזת ה-JSON עם זוג מפתח-ערך "Me" : 800 נוסף אליו. ```
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!
