Use the json module to translate Python objects to and from a JSON serialized string representation in memory:
import json name = dict(first='Bob', last='Smith') rec = dict(name1=name, job=['dev', 'mgr'], age=40.5) print( json.dumps(rec) ) S = json.dumps(rec) # from w w w . j ava 2 s . c o m print( S ) O = json.loads(S) print( O ) print( O == rec )
To translate Python objects to and from JSON data strings in files.
import json rec = dict(name=name, job=['dev', 'mgr'], age=40.5) json.dump(rec, fp=open('testjson.txt', 'w'), indent=4) print(open('testjson.txt').read()) P = json.load(open('testjson.txt')) print( P )# from www. j av a 2s .c o m