簡體   English   中英

如何從JSON響應中提取特定部分?

[英]How to extract a specific part from a JSON response?

我有類似的代碼

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堅持認為這實際上是一個列表。

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

#prints 'noun'
for i in range(len(results)):
    print(results[i]['part_of_speech'])

noun
noun
noun
noun
noun
noun
noun
noun
noun
noun

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM