2017-06-02 14 views
1

私は困惑しており、このエラーを解決する方法を知らない。 JSONレスポンスリスト内のすべての名前を取得しようとしています。リストの添字は整数でなければなりません。 strではないJSONの応答

私のコードは次のようになっています。

def extract_strucutres_ids(expected_structures): 
    response = requests.get(JIRA_REST + "/structures", verify=False) 
    response = response.json() 
    for structure in response['structures']: 
     print structure['name'] 

Jsonの応答は次のようになります。

{ 
    "structures": [{ 
      "id": 165, 
      "name": "6.2 External Notifications Refactor", 
      "description": "" 
     }, { 
      "id": 364, 
      "name": "6.4 Day/Night Mode and Idle Scene Mode", 
      "description": "", 
      "readOnly": true 
     }, { 
      "id": 140, 
      "name": "ACC 5 Regression", 
      "description": "" 
     } 
    ] 
} 

私はList indicies must be integers, not strを得続けます。 Pythonのバージョン2.7.10

+0

まず、response ['strucutres'] 'にタイプミスを修正してください。構造体でなければなりません。 – codekaizer

+1

'response'は辞書のように見えるかもしれませんが、おそらく文字列です。これは 'response'の型を出力することで確認できます:' print(type(response)) '。 –

答えて

2

これを試してみてください -

import json 

def extract_strucutres_ids(expected_structures): 
    response = requests.get(JIRA_REST + "/structures", verify=False) 
    if response.status_code==200: 
     response_json = json.loads(response.text) 
     for structure in response_json['structures']: 
      print structure['name'] 
    else: 
     print("Response is {}".format(response.status_code)) 

それが働いていた場合、私に教えてください!

+0

それは動作します!ありがとう! –

0

使用json.loads()

response = requests.get(..) 
response = json.loads(response.text) # response.text is a string 
for structure in response['structures']: 
    # Do something 
+0

私は、応答レスポンス= json.loads(response.json())にエラーが発生しました –

+0

@ j.doe、代わりに 'response.text'を使用するように私の答えを修正しました – codekaizer

関連する問題