2016-10-04 16 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" 
    } 
] 

を私がしたいすべてが応答から各「品詞」の値を取得し、リストに押し込みですが、Iこれを行う方法を理解することはできません。

私は解析に失敗しましたが、実際にはこれがリストであると主張しています。

+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 
関連する問題