urllib表达我想要的JSON数据

时间:2016-07-22 21:24:11

标签: python json api python-3.x urlfetch

以下是我在下面的代码中提取的一大块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结果中提供的定义。我不知道如何表达这一点,我得到的列表索引必须是整数或切片,而不是str'我感觉错误=数据['感官']。

#!/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'])

1 个答案:

答案 0 :(得分:0)

sense实际上是一个包含单个元素的列表,一个字典。包含的字典具有您想要的键值对。

例如:

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