2016-07-22 22 views
1

這裏是一大片的JSON數據,我在我下面的代碼獲取:urllib的表達哪塊JSON數據的我渴望

{ 
    "status": 200, 
    "offset": 0, 
    "limit": 10, 
    "count": 8, 
    "total": 8, 
    "url": "/v2/dictionaries/ldoce5/entries?headword=extra", 
    "results": [ 
    { 
     "datasets": [ 
     "ldoce5", 
     "dictionary" 
     ], 
     "headword": "extra", 
     "homnum": 3, 
     "id": "cqAFDjvvYg", 
     "part_of_speech": "adverb", 
     "senses": [ 
     { 
      "collocation_examples": [ 
      { 
       "collocation": "one/a few etc extra", 
       "example": { 
       "audio": [ 
        { 
        "type": "example", 
        "url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-001627480.mp3" 
        } 
       ], 
       "text": "I got a few extra in case anyone else decides to come." 
       } 
      } 
      ], 
      "definition": [ 
      "in addition to the usual things or the usual amount" 
      ], 
      "examples": [ 
      { 
       "audio": [ 
       { 
        "type": "example", 
        "url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-001627477.mp3" 
       } 
       ], 
       "text": "They need to offer something extra to attract customers." 
      } 
      ] 
     } 
     ], 
     "url": "/v2/dictionaries/entries/cqAFDjvvYg" 
    }, 
    { 
     "datasets": [ 
     "ldoce5", 
     "dictionary" 
     ], 
     "headword": "extra-", 
     "id": "cqAFDk1BDw", 
     "part_of_speech": "prefix", 
     "pronunciations": [ 
     { 
      "audio": [ 
      { 
       "lang": "American English", 
       "type": "pronunciation", 
       "url": "/v2/dictionaries/assets/ldoce/us_pron/extra__pre.mp3" 
      } 
      ], 
      "ipa": "ekstrə" 
     } 
     ], 
     "senses": [ 
     { 
      "definition": [ 
      "outside or beyond" 
      ], 
      "examples": [ 
      { 
       "audio": [ 
       { 
        "type": "example", 
        "url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-001832333.mp3" 
       } 
       ], 
       "text": "extragalactic (=outside our galaxy)" 
      } 
      ] 
     } 
     ], 
     "url": "/v2/dictionaries/entries/cqAFDk1BDw" 
    }, 
    { 
     "datasets": [ 
     "ldoce5", 
     "dictionary" 
     ], 
     "headword": "extra", 
     "homnum": 1, 
     "id": "cqAFDjpNZQ", 
     "part_of_speech": "adjective", 
     "pronunciations": [ 
     { 
      "audio": [ 
      { 
       "lang": "British English", 
       "type": "pronunciation", 
       "url": "/v2/dictionaries/assets/ldoce/gb_pron/extra_n0205.mp3" 
      }, 
      { 
       "lang": "American English", 
       "type": "pronunciation", 
       "url": "/v2/dictionaries/assets/ldoce/us_pron/extra1.mp3" 
      } 
      ], 
      "ipa": "ˈekstrə" 
     } 
     ], 
     "senses": [ 
     { 
      "collocation_examples": [ 
      { 
       "collocation": "an extra ten minutes/three metres etc", 
       "example": { 
       "audio": [ 
        { 
        "type": "example", 
        "url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-001202489.mp3" 
        } 
       ], 
       "text": "I asked for an extra two weeks to finish the work." 
       } 
      } 
      ], 
      "definition": [ 
      "more of something, in addition to the usual or standard amount or number" 
      ], 
      "examples": [ 
      { 
       "audio": [ 
       { 
        "type": "example", 
        "url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-001202484.mp3" 
       } 
       ], 
       "text": "Could you get an extra loaf of bread?" 
      } 
      ], 
      "gramatical_info": { 
      "type": "only before noun" 
      } 
     } 
     ], 
     "url": "/v2/dictionaries/entries/cqAFDjpNZQ" 
    }, 
    { 
     "datasets": [ 
     "ldoce5", 
     "dictionary" 
     ], 
     "headword": "extra", 
     "homnum": 2, 
     "id": "cqAFDjsQjH", 
     "part_of_speech": "pronoun", 
     "senses": [ 
     { 
      "collocation_examples": [ 
      { 
       "collocation": "pay/charge/cost etc extra", 
       "example": { 
       "audio": [ 
        { 
        "type": "example", 
        "url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-001202499.mp3" 
        } 
       ], 
       "text": "I earn extra for working on Sunday." 
       } 
      } 
      ], 
      "definition": [ 
      "an amount of something, especially money, in addition to the usual, basic, or necessary amount" 
      ], 
      "synonym": "more" 
     } 
     ], 
     "url": "/v2/dictionaries/entries/cqAFDjsQjH" 
    }, 
    { 
     "datasets": [ 
     "ldoce5", 
     "dictionary" 
     ], 
     "headword": "extra", 
     "homnum": 4, 
     "id": "cqAFDjyTn8", 
     "part_of_speech": "noun", 
     "senses": [ 
     { 
      "definition": [ 
      "something which is added to a basic product or service that improves it and often costs more" 
      ], 
      "examples": [ 
      { 
       "audio": [ 
       { 
        "type": "example", 
        "url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-001202524.mp3" 
       } 
       ], 
       "text": "Tinted windows and a sunroof are optional extras(=something that you can choose to have or not)." 
      } 
      ] 
     } 
     ], 
     "url": "/v2/dictionaries/entries/cqAFDjyTn8" 
    }, 
    { 
     "datasets": [ 
     "ldoce5", 
     "dictionary" 
     ], 
     "headword": "extra virgin", 
     "id": "cqAFDmV2Jw", 
     "part_of_speech": "adjective", 
     "senses": [ 
     { 
      "definition": [ 
      "extra virgin olive oil comes from olives that are pressed for the first time, and is considered to be the best quality olive oil" 
      ] 
     } 
     ], 
     "url": "/v2/dictionaries/entries/cqAFDmV2Jw" 
    }, 
    { 
     "datasets": [ 
     "ldoce5", 
     "dictionary" 
     ], 
     "headword": "extra time", 
     "id": "cqAFDmGZyQ", 
     "part_of_speech": "noun", 
     "senses": [ 
     { 
      "american_equivalent": "overtime", 
      "definition": [ 
      "a period, usually of 30 minutes, added to the end of a football game in some competitions if neither team has won after normal time" 
      ], 
      "examples": [ 
      { 
       "audio": [ 
       { 
        "type": "example", 
        "url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-001627835.mp3" 
       } 
       ], 
       "text": "The match went into extra time." 
      } 
      ], 
      "geography": "especially British English", 
      "gramatical_examples": [ 
      { 
       "examples": [ 
       { 
        "audio": [ 
        { 
         "type": "example", 
         "url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-001627834.mp3" 
        } 
        ], 
        "text": "Beckham scored in extra time." 
       } 
       ], 
       "pattern": "in extra time" 
      } 
      ] 
     } 
     ], 
     "url": "/v2/dictionaries/entries/cqAFDmGZyQ" 
    }, 
    { 
     "datasets": [ 
     "ldoce5", 
     "dictionary" 
     ], 
     "headword": "extra-sensory perception", 
     "id": "cqAFDm6ceW", 
     "part_of_speech": "noun", 
     "senses": [ 
     { 
      "definition": [ 
      "ESP" 
      ] 
     } 
     ], 
     "url": "/v2/dictionaries/entries/cqAFDm6ceW" 
    } 
    ] 
} 

我想抓住並打印在JSON結果所提供的定義。我不知道如何表達這一點,並得到一個'列表索引必須是整數或切片,而不是'我的意義=數據['感官']錯誤。

#!/usr/bin/env python 

import urllib.request 
import json 

wp = urllib.request.urlopen("http://api.pearson.com/v2/dictionaries/ldoce5/entries?headword=extra").read().decode('utf8') 
jsonData=json.loads(wp) 
data=jsonData['results'] 

for item in data: 
    sense = data['senses'] 
    print(senses['definition']) 

回答

0

sense實際上是一個包含單個元素,一個字典的列表。包含的字典有你想要的鍵值對。

例如:

for item in data: 
    sense = data['senses'][0] 
    print(sense['definition']) 
+0

邁克爾,謝謝你的見解。這幫助我瞭解了我的代碼中的所有錯誤。最後一個循環實際上看起來像這樣: 對於數據中的項目: print(item ['senses'] [0] ['definition']) 請注意,我還通過表達數據引用了感官的錯誤[ '感官']而不是適當的項目['感官'] – kelemeno