teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

d

2019/05/30 07:57

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -1,4 +1,4 @@
1
- `Resources/*/PolicyDocument/Properties/PolicyDocument/Statement/0/Action` にアクセスしたい場合は、json['Resources'] に対してitems() 関数で for ループすればよいです。
1
+ `Resources/*/PolicyDocument/Properties/PolicyDocument/Statement/0/Action` にアクセスしたい場合は、json['Resources'] 配下の任意の要素アクセスするにはvalues() 関数で for ループすればよいです。
2
2
 
3
3
  ```python
4
4
  import json
@@ -6,7 +6,7 @@
6
6
  with open("test.json") as f:
7
7
  data = json.load(f)
8
8
 
9
- for key, val in data["Resources"].items():
9
+ for val in data["Resources"].values():
10
10
  print(val["Properties"]["PolicyDocument"]["Statement"][0]["Action"])
11
11
  ```
12
12
 
@@ -15,7 +15,7 @@
15
15
  cloudformation:*
16
16
  ```
17
17
 
18
- items() 関数の使い方は dict と同じです。
18
+ values() 関数の使い方は dict と同じです。
19
19
 
20
20
  ```python
21
21
  import json
@@ -28,8 +28,8 @@
28
28
  """
29
29
 
30
30
  data = json.loads(j)
31
- for key, val in data.items():
31
+ for val in data.values():
32
- print(key, val)
32
+ print(val)
33
- # color red
33
+ # red
34
- # value #f00
34
+ # #f00
35
35
  ```