Python/JSON形式のオブジェクトの取扱
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
#topicpath
----
#contents
$ 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': 'applicat...
{'Accept': 'application/json', 'Content-type': 'applicat...
{'Accept': 'application/json', 'Content-type': 'applicat...
application/json
application/json
application/json
----
この記事は
#vote(おもしろかった,そうでもない)
#comment
#topicpath
SIZE(10){現在のアクセス:&counter;}
終了行:
#topicpath
----
#contents
$ 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': 'applicat...
{'Accept': 'application/json', 'Content-type': 'applicat...
{'Accept': 'application/json', 'Content-type': 'applicat...
application/json
application/json
application/json
----
この記事は
#vote(おもしろかった,そうでもない)
#comment
#topicpath
SIZE(10){現在のアクセス:&counter;}
ページ名: