Top / Python / JSON形式のオブジェクトの取扱

$ 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

この記事は

選択肢 投票
おもしろかった 0  
そうでもない 0  

Top / Python / JSON形式のオブジェクトの取扱

現在のアクセス:1007


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2018-07-23 (月) 15:47:56 (2104d)