在Python中按值获取JSON列表索引

时间:2016-06-28 17:04:45

标签: python json django

我有一个网站,我正在使用Django制作API请求以获取有关汽车的信息。这些请求由JS事件触发,这些事件将转到连接到视图的已创建URL。在我看来,我发出了实际的API请求,并使用.json()方法从响应中获取返回的JSON。

我遇到的麻烦是从此回复中获得某个索引/值。

以下是我将收到的回复示例:

{
    "equipment": [{
        "id": "20047746549",
        "name": "Specifications",
        "equipmentType": "OTHER",
        "availability": "STANDARD",
        "attributes": [{
            "name": "Aerodynamic Drag (cd)",
            "value": "0.26"
        }, {
            "name": "Ege Highway Mpg",
            "value": "29"
        }, {
            "name": "Epa Combined Mpg",
            "value": "23"
        }, {
            "name": "Epa City Mpg",
            "value": "20"
        }, {
            "name": "Curb Weight",
            "value": "3957"
        }, {
            "name": "Turning Diameter",
            "value": "39.0"
        }, {
            "name": "Manufacturer 0 60mph Acceleration Time (seconds)",
            "value": "6.6"
        }, {
            "name": "Epa Highway Mpg",
            "value": "29"
        }, {
            "name": "Tco Curb Weight",
            "value": "3957"
        }, {
            "name": "Ege Combined Mpg",
            "value": "23"
        }, {
            "name": "Fuel Capacity",
            "value": "19.8"
        }, {
            "name": "Ege City Mpg",
            "value": "20"
        }]
    }],
    "equipmentCount": 1
}

我想要获得的是与"value": "3957"属性相对应的"name": "Curb Weight"

我考虑这样做的方式(在Python中)是jsonResponse['equipment'][0]['attributes'][4]['value']但是对于此Curb Weight属性,索引并不总是相同。有时索引是5,有时是6,等等。

无论如何,根据它的"name"键的值来获取此“路缘权重”属性或任何其他属性?

0 个答案:

没有答案