$ cat ObjSample.py #!/usr/bin/env python # -*- coding: utf-8 -*- import sys import json def main(args): # Dictioinary 型を使ってオブジェクトを定義 headers2 = dict() headers2["Accept"] = "application/json" headers2["Content-type"] = "application/json" # JSON形式で、オブジェクトを定義 headers1 ={ "Accept": "application/json", "Content-type": "application/json" } # JSON文字列から、オブジェクトを定義 headers3str = ''' { "Accept": "application/json", "Content-type": "application/json" } ''' headers3 = json.loads(headers3str) print(headers1) print(headers2) print(headers3) print(headers1["Accept"]) print(headers2["Accept"]) print(headers3["Content-type"]) if __name__ == "__main__": main(sys.argv)
実行結果:
$ sudo python3 ObjSample.py {'Accept': 'application/json', 'Content-type': 'application/json'} {'Accept': 'application/json', 'Content-type': 'application/json'} {'Accept': 'application/json', 'Content-type': 'application/json'} application/json application/json application/json