urllib表示我设计的JSON数据

2024-09-29 23:17:24 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我在代码中获取的一大块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'错误为我的感觉=数据['senses']。你知道吗

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

Tags: oftourlexampletypeextramp3audio

热门问题