50 lines
1.3 KiB
Python
50 lines
1.3 KiB
Python
|
'''
|
||
|
Parsing functions :
|
||
|
obj = load(file) # file
|
||
|
obj = loads(string) # string suppost
|
||
|
|
||
|
Serialization functions :
|
||
|
dump(obj, file)
|
||
|
str = dumps(obj)
|
||
|
|
||
|
Conversion convention
|
||
|
--Serialization--> --Parsing-->
|
||
|
Python object | JSON Repr | Python object
|
||
|
------------------------------------------------------------
|
||
|
dict | object | dict
|
||
|
list, tuple | array | list
|
||
|
str | string | str
|
||
|
int, long, float, Enums | number | int, float- depends
|
||
|
True | true | True
|
||
|
False | false | False
|
||
|
None | null | None
|
||
|
'''
|
||
|
# Process JSON data returned from a server
|
||
|
|
||
|
# TODO : use the JSON module
|
||
|
import json
|
||
|
|
||
|
def main() :
|
||
|
# define a python dictionary
|
||
|
pythonData = {
|
||
|
"sandwich" : "Reuben",
|
||
|
"toasted" : True,
|
||
|
"toppings": ["Thousand Island Dressing",
|
||
|
"Sauerkraut",
|
||
|
"Pickles"
|
||
|
],
|
||
|
"price" : 8.99
|
||
|
}
|
||
|
|
||
|
# TODO : parse the jason data using loads
|
||
|
jsonStr = json.dumps(pythonData, indent=4)
|
||
|
|
||
|
|
||
|
# TODO : print information from the data structure
|
||
|
print('JSON data :----------')
|
||
|
print(jsonStr)
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
main()
|