、あなたはJSONモジュールをインポートして、ダンプ・メソッドを使用して、例えば、負荷の方法を使用することができますJSONをデコードすることができます。
import json
json_data_encoding = json.dumps(
{'user_id': 1, 'name': 'bob', 'comment': 'this is a comment'}
)
print(json_data_encoding)
# output: {"name": "bob", "user_id": 1, "comment": "this is a comment"}
json_data_decoding = json.loads(json_data_encoding)
print(json_data_decoding['name'])
# output: bob
従うリストを生成するために対処するためには、下のコードでは、リストをループする方法も示しています。
list_json_data_encoding = json.dumps([
{'user_id': 1, 'name': 'bob', 'comment': 'this is a comment'},
{'user_id': 2, 'name': 'tom', 'comment': 'this is another comment'}
]
)
print(list_json_data_encoding)
# output: [{"name": "bob", "user_id": 1, "comment": "this is a comment"}, {"name": "tom", "user_id": 2, "comment": "this is another comment"}]
list_json_data_decoding = json.loads(list_json_data_encoding)
for json_data_decoded in list_json_data_decoding:
print(json_data_decoded['name'])
# output1: bob
# output2: tom
希望します。
これはあなたが探しているものですか?
import json
script_users = map(str, input("Please enter your user names, seperated by space with no comma: ").split())
users = list(set(script_users))
list_users = []
for user in users:
list_users.append({'user_id': 1, 'name': user, 'comment': 'this is a comment'})
json_user_list_encoding = json.dumps(list_users)
print(json_user_list_encoding)
# this will return a json list where user_id will always be 1 and comment will be 'this is a comment',
# the name will change for every user inputed by a space
json_user_list_decoding = json.loads(json_user_list_encoding)
for json_user_decoded in json_user_list_decoding:
print(json_user_decoded['name'])
# output: return the names as the where entered originally
PythonデータをJSON形式で印刷するには、 'json'モジュールを使用します。 – Barmar
https://docs.python.org/2/library/json.htmlを参照してください。 – Barmar