LoginSignup
0
1

More than 1 year has passed since last update.

json

Last updated at Posted at 2020-03-10
import json
j = {
    "employee":
        [
            {"id": 111, "name": "Mike"},
            {"id": 222, "name": "Nancy"}
        ]
}

print(j)
print("###########")

#python内で読み書きするときはsがつくので注意
print('jsonの場合ダブルクォートで囲まれる\n' + json.dumps(j))
a = json.dumps(j)
print('@@@@@@@@@@')
print(json.loads(a))

#ファイルを読み書きするときはsがつかない
with open('test.json', 'w') as f:
    json.dump(j, f)
print('###########')
with open('test.json', 'r') as f:
    print(json.load(f))

出力:

{'employee': [{'id': 111, 'name': 'Mike'}, {'id': 222, 'name': 'Nancy'}]}
###########
jsonの場合ダブルクォートで囲まれる
{"employee": [{"id": 111, "name": "Mike"}, {"id": 222, "name": "Nancy"}]}
@@@@@@@@@@
{'employee': [{'id': 111, 'name': 'Mike'}, {'id': 222, 'name': 'Nancy'}]}
###########
{'employee': [{'id': 111, 'name': 'Mike'}, {'id': 222, 'name': 'Nancy'}]}

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1