1. JSON Library
1 2 3 4 5 6 7 | import json def pythonToJson(s): return json.dumps(s, indent=4) #return str def jsonToPython(s): return json.loads(s) #return dict |
2. JSON Library Test Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | def pythonToJsonTest(): customer = { 'id': 152352, 'name': 'alice', 'history': [ {'date': '2015-03-11', 'item': 'iPhone'}, {'date': '2016-02-23', 'item': 'Monitor'}, ] } jsonString = pythonToJson(customer) print(jsonString) print(type(jsonString)) # class str def jsonToPythonTest(): jsonString = '{"name": "alice", "id": 152352, "history": [{"date": "2015-03-11", "item": "iPhone"}, {"date": "2016-02-23", "item": "Monitor"}]}' dict = jsonToPython(jsonString) print(dict['name']) for h in dict['history']: print(h['date'], h['item']) |
댓글 없음:
댓글 쓰기