Page 305 -
P. 305
mobile app development
JSON is an established web standard that comes preinstalled with Python 2 and Python 3. The JSON API is not that
much different to the one used by pickle:
Import the JSON library.
>>> import json
>>> names = ['John', ['Johnny', 'Jack'], 'Michael', ['Mike', 'Mikey', 'Mick']]
>>> names Create a list of lists.
['John', ['Johnny', 'Jack'], 'Michael', ['Mike', 'Mikey', 'Mick']]
Transform the Python list-of-lists into a JSON list of lists.
>>> to_transfer = json.dumps(names)
>>> to_transfer The format is similar,
but different.
'["John", ["Johnny", "Jack"], "Michael", ["Mike", "Mikey", "Mick"]]'
Transform the JSON list of lists back
>>> from_transfer = json.loads(to_transfer) into one that Python understands.
>>> from_transfer
['John', ['Johnny', 'Jack'], 'Michael', ['Mike', 'Mikey', 'Mick']]
The new data is exactly the same
>>> names as the original list of lists.
['John', ['Johnny', 'Jack'], 'Michael', ['Mike', 'Mikey', 'Mick']]
Add a new function to the athletemodel module that, when
called, returns the list of athlete names as a string.
Call the new function get_names_from_store().
you are here 4 269