2016-10-04 142 views
0

我有這樣如何從JSON響應中提取特定部分?

import requests 
dict_api = "http://api.pearson.com/v2/dictionaries/entries?headword=" 
r = requests.get(dict_api + word) 
results = r.json()["results"] 

這個代碼給了我這樣的:

[ 
    { 
     "senses": [ 
      { 
       "translation": "\\u6c99\\u62c9\\uff0c\\u8272\\u62c9\\uff0c\\u6c99\\u5f8b\\u3014\\u7528\\u751f\\u83dc\\u3001\\u9ec3\\u74dc\\u3001\\u756a\\u8304\\u7b49\\u751f\\u7684\\u852c\\u83dc\\u4f5c\\u539f\\u6599\\u7684\\u6dbc\\u62cc\\u83dc\\u3015" 
      } 
     ], 
     "url": "/v2/dictionaries/entries/cqAJbKG6Bx", 
     "part_of_speech": "noun", 
     "datasets": [ 
      "ldec", 
      "dictionary" 
     ], 
     "headword": "salad", 
     "id": "cqAJbKG6Bx" 
    }, 
    { 
     "pronunciations": [ 
      { 
       "ipa": "\\u02c8s\u5adcu0259d", 
       "audio": [ 
        { 
         "url": "/v2/dictionaries/assets/ldoce/gb_pron/salad_las2_br.mp3", 
         "lang": "British English", 
         "type": "pronunciation" 
        }, 
        { 
         "url": "/v2/dictionaries/assets/ldoce/us_pron/salad.mp3", 
         "lang": "American English", 
         "type": "pronunciation" 
        } 
       ] 
      } 
     ], 
     "senses": [ 
      { 
       "definition": [ 
        "a mixture of raw vegetables, especially lettuce, cucumber, and tomato" 
       ], 
       "collocation_examples": [ 
        { 
         "collocation": "toss a salad", 
         "example": { 
          "audio": [ 
           { 
            "url": "/v2/dictionaries", 
            "type": "example" 
           } 
          ] 
         } 
        } 
       ], 
       "examples": [ 
        { 
         "text": "Would you like some salad with your pasta?", 
         "audio": [ 
          { 
           "url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-000741011.mp3", 
           "type": "example" 
          } 
         ] 
        } 
       ] 
      } 
     ], 
     "url": "/v2/dictionaries/entries/cqAFq6n9JR", 
     "part_of_speech": "noun", 
     "datasets": [ 
      "ldoce5", 
      "dictionary" 
     ], 
     "headword": "salad", 
     "id": "cqAFq6n9JR" 
    }, 
    { 
     "senses": [ 
      { 
       "definition": "a mixture of vegetables, eaten cold", 
       "examples": [ 
        { 
         "text": "a tomato salad" 
        } 
       ] 
      } 
     ], 
     "url": "/v2/dictionaries/entries/cqARFW4JGH", 
     "part_of_speech": "noun", 
     "datasets": [ 
      "wordwise", 
      "dictionary" 
     ], 
     "headword": "salad", 
     "id": "cqARFW4JGH" 
    }, 
    { 
     "pronunciations": [ 
      { 
       "ipa": "\\u02c8s\u5adcu0259d" 
      } 
     ], 
     "senses": [ 
      {} 
     ], 
     "url": "/v2/dictionaries/entries/csTMd1V83W", 
     "part_of_speech": "noun", 
     "datasets": [ 
      "laes", 
      "dictionary" 
     ], 
     "headword": "salad", 
     "id": "csTMd1V83W" 
    }, 
    { 
     "senses": [ 
      { 
       "definition": "rou groentemengsel", 
       "translations": [ 
        { 
         "text": "slaai" 
        } 
       ] 
      } 
     ], 
     "url": "/v2/dictionaries/entries/cqAG06X6Kc", 
     "part_of_speech": "noun", 
     "datasets": [ 
      "leasd", 
      "dictionary" 
     ], 
     "headword": "salad", 
     "id": "cqAG06X6Kc" 
    }, 
    { 
     "senses": [ 
      { 
       "definition": "a mixture of raw vegetables, usually including lettuce", 
       "examples": [ 
        { 
         "text": "a tomato and cucumber salad" 
        } 
       ] 
      } 
     ], 
     "url": "/v2/dictionaries/entries/cs1tApKyAq", 
     "part_of_speech": "noun", 
     "datasets": [ 
      "laad3", 
      "dictionary" 
     ], 
     "headword": "salad", 
     "id": "cs1tApKyAq" 
    }, 
    { 
     "pronunciations": [ 
      { 
       "lang": "American English", 
       "ipa": "\\u02c8s\u5adcu0259d" 
      } 
     ], 
     "senses": [ 
      { 
       "definition": [ 
        "dish of mixed raw vegetables" 
       ], 
       "translations": [ 
        { 
         "text": [ 
          "salada" 
         ], 
         "example": [ 
          { 
           "text": "a salad of tomatoes, lettuce and cucumber", 
           "translation": { 
            "text": [ 
             "uma salada de tomate, alface e pepino" 
            ] 
           } 
          } 
         ] 
        } 
       ] 
      } 
     ], 
     "url": "/v2/dictionaries/entries/ct59xTBDRn", 
     "part_of_speech": "noun", 
     "datasets": [ 
      "brep", 
      "dictionary" 
     ], 
     "headword": "salad", 
     "id": "ct59xTBDRn" 
    }, 
    { 
     "pronunciations": [ 
      { 
       "ipa": "\\u02c8s\u5adcu0259d" 
      } 
     ], 
     "senses": [ 
      { 
       "definition": [ 
        "a mixture of vegetables eaten cold and usually raw" 
       ], 
       "examples": [ 
        { 
         "text": "a salad of lettuce, tomatoes, and cucumber" 
        } 
       ] 
      } 
     ], 
     "url": "/v2/dictionaries/entries/cvfdktDye8", 
     "part_of_speech": "noun", 
     "datasets": [ 
      "lasde", 
      "dictionary" 
     ], 
     "headword": "salad", 
     "id": "cvfdktDye8" 
    }, 
    { 
     "senses": [ 
      { 
       "definition": "a salad made with lettuce and other raw green vegetables" 
      } 
     ], 
     "url": "/v2/dictionaries/entries/cs1saJp1NK", 
     "part_of_speech": "noun", 
     "datasets": [ 
      "laad3", 
      "dictionary" 
     ], 
     "headword": "green salad", 
     "id": "cs1saJp1NK" 
    }, 
    { 
     "senses": [ 
      { 
       "translation": "\\u8a00\\u8a9e\\u96dc\\u4e82\\uff0c\\u8a00\\u8a9e\\u856a\\u96dc" 
      } 
     ], 
     "url": "/v2/dictionaries/entries/cqAJqE3gvE", 
     "part_of_speech": "noun", 
     "datasets": [ 
      "ldec", 
      "dictionary" 
     ], 
     "headword": "word salad", 
     "id": "cqAJqE3gvE" 
    } 
] 

所有我想要做的就是從響應中的每個「part_of_speech」值,並將其推入清單,但我無法弄清楚如何做到這一點。

我沒有成功嘗試解析和python堅持這實際上是一個列表。

+4

考慮縮進你JSON以提高可讀性。 –

+2

如果Python堅持它是一個列表,它是這樣做的 - 有一個原因 - 在這種情況下,主要原因是它*是*列表,*包含* JSON對象。 – n1c9

回答

3
print(results[0]['part_of_speech']) 

#prints 'noun' 
+0

我現在覺得自己像個白癡!謝謝您的回答。 –

1
for i in range(len(results)): 
    print(results[i]['part_of_speech']) 

noun 
noun 
noun 
noun 
noun 
noun 
noun 
noun 
noun 
noun