File: class/Extras/Other/PriorClasses/scotts-sep12/json-demo.txt
Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> >>> >>> rec = {'name': {'first': 'Bob', 'last': 'Smith'}, 'job': ['dev','mgr'], 'age': 40.5} >>> >>> >>> import pprint >>> pprint.pprint(rec) {'age': 40.5, 'job': ['dev', 'mgr'], 'name': {'first': 'Bob', 'last': 'Smith'}} >>> >>> >>> import json >>> json.dumps(rec) '{"age": 40.5, "job": ["dev", "mgr"], "name": {"last": "Smith", "first": "Bob"}}' >>> >>> >>> json.dumps(rec, indent=4) '{\n "age": 40.5, \n "job": [\n "dev", \n "mgr"\n ], \n "name": {\n "last": "Smith", \n "first": "Bob"\n }\n}' >>> >>> >>> print json.dumps(rec, indent=4) { "age": 40.5, "job": [ "dev", "mgr" ], "name": { "last": "Smith", "first": "Bob" } } >>> >>> >>> print json.dump(rec, fp=open('testjson.txt','w'), indent=4) None >>> >>> open('testjson.txt').read() '{\n "age": 40.5, \n "job": [\n "dev", \n "mgr"\n ], \n "name": {\n "last": "Smith", \n "first": "Bob"\n }\n}' >>> >>> print open('testjson.txt').read() { "age": 40.5, "job": [ "dev", "mgr" ], "name": { "last": "Smith", "first": "Bob" } } >>> >>> >>> bob = json.load(open('testjson.txt')) >>> bob {u'age': 40.5, u'job': [u'dev', u'mgr'], u'name': {u'last': u'Smith', u'first': u'Bob'}} >>> >>> pprint.pprint(bob) {u'age': 40.5, u'job': [u'dev', u'mgr'], u'name': {u'first': u'Bob', u'last': u'Smith'}} >>> >>> pprint.pprint(bob, indent=4) { u'age': 40.5, u'job': [u'dev', u'mgr'], u'name': { u'first': u'Bob', u'last': u'Smith'}} >>> >>> pprint.pprint(rec, indent=4) { 'age': 40.5, 'job': ['dev', 'mgr'], 'name': { 'first': 'Bob', 'last': 'Smith'}} >>> >>> rec {'age': 40.5, 'job': ['dev', 'mgr'], 'name': {'last': 'Smith', 'first': 'Bob'}} >>> >>> S = json.dumps(rec) >>> S '{"age": 40.5, "job": ["dev", "mgr"], "name": {"last": "Smith", "first": "Bob"}}' >>> >>> O = json.loads(S) >>> O {u'age': 40.5, u'job': [u'dev', u'mgr'], u'name': {u'last': u'Smith', u'first': u'Bob'}} >>> >>> pprint.pprint(O) {u'age': 40.5, u'job': [u'dev', u'mgr'], u'name': {u'first': u'Bob', u'last': u'Smith'}}