LoginSignup
1
0

More than 3 years have passed since last update.

pythonでjsonの配列にアクセスする

Posted at

pythonでjsonの配列にアクセスする時のメモ

jsonファイル

test.json
{
    "section1":{
        "key":"key1",
        "number": [ 
                    ["a", "b"],
                    ["c", "d"],
                    ["e", "f"]
                  ]
    },

    "section2":{
        "key":"key2",
        "number": 2
    }
}

pythonファイル

test.py
import json

json_open = open('test.json', 'r')
json_load = json.load(json_open)

print(json_load)
print(json_load["section1"]["number"][0])
print(json_load["section1"]["number"][1])
print(json_load["section1"]["number"][2])
print(json_load["section1"]["number"][0][0])
print(json_load["section1"]["number"][0][1])

実行

$ python test.py
{'section1': {'key': 'key1', 'number': [['a', 'b'], ['c', 'd'], ['e', 'f']]}, 'section2': {'key': 'key2', 'number': 2}}
['a', 'b']
['c', 'd']
['e', 'f']
a
b

参考

非エンジニアのためのJSON入門

1
0
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
1
0