如何从JSON响应中提取特定部分?

时间:2016-10-04 16:03:37

标签: python json python-requests

我有像

这样的代码
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坚持认为这实际上是一个列表。

2 个答案:

答案 0 :(得分:3)

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

#prints 'noun'

答案 1 :(得分:1)

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

noun
noun
noun
noun
noun
noun
noun
noun
noun
noun