如何在Python中从Json数组检索特定记录?

时间:2018-08-30 12:13:15

标签: python json python-3.x

我有以下Json字符串:

{
    "count": 30,
    "previous": "None",
    "results": [
        {
            "detail": "http://localhost:3000/building/53/",
            "bounds": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            2.2017,
                            41.4045
                        ],
                        [
                            2.202,
                            41.4047
                        ],
                        [
                            2.2022,
                            41.4046
                        ],
                        [
                            2.2019,
                            41.4044
                        ],
                        [
                            2.2017,
                            41.4045
                        ]
                    ]
                ]
            },
            "id": 53,
            "name": "Triat VAL",
            "site": {
                "customer": {
                    "user": "fina",
                    "id": 1,
                    "name": "Test devel"
                },
                "id": 2,
                "name": "Dev - Triat"
            }
        },
        {
            "detail": "http://localhost:3000/building/1/",
            "bounds": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            2.1256459247880657,
                            41.488959573689165
                        ],
                        [
                            2.1253611647528783,
                            41.48957169540171
                        ],
                        [
                            2.1258887411935175,
                            41.48971045305678
                        ],
                        [
                            2.1261734969147086,
                            41.48909833001004
                        ],
                        [
                            2.1256459247880657,
                            41.488959573689165
                        ]
                    ]
                ]
            },
            "id": 1,
            "name": "Laboratori",
            "site": {
                "customer": {
                    "user": "fina",
                    "id": 1,
                    "name": "Test devel"
                },
                "id": 2,
                "name": "Dev - Triat"
            }
        },
        {
            "detail": "http://localhost:3000/building/55/",
            "bounds": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            2.2017,
                            41.4045
                        ],
                        [
                            2.202,
                            41.4047
                        ],
                        [
                            2.2022,
                            41.4046
                        ],
                        [
                            2.2019,
                            41.4044
                        ],
                        [
                            2.2017,
                            41.4045
                        ]
                    ]
                ]
            },
            "id": 55,
            "name": "Prueba 1",
            "site": {
                "customer": {
                    "user": "fina",
                    "id": 1,
                    "name": "Test devel"
                },
                "id": 2,
                "name": "Dev - Triat"
            }
        },
        {
            "detail": "http://localhost:3000/building/72/",
            "bounds": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            2.2017,
                            41.4045
                        ],
                        [
                            2.202,
                            41.4047
                        ],
                        [
                            2.2022,
                            41.4046
                        ],
                        [
                            2.2019,
                            41.4044
                        ],
                        [
                            2.2017,
                            41.4045
                        ]
                    ]
                ]
            },
            "id": 72,
            "name": "Prueba 1",
            "site": {
                "customer": {
                    "user": "fina",
                    "id": 1,
                    "name": "Test devel"
                },
                "id": 2,
                "name": "Dev - Triat"
            }
        },
        {
            "detail": "http://localhost:3000/building/73/",
            "bounds": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            2.2017,
                            41.4045
                        ],
                        [
                            2.202,
                            41.4047
                        ],
                        [
                            2.2022,
                            41.4046
                        ],
                        [
                            2.2019,
                            41.4044
                        ],
                        [
                            2.2017,
                            41.4045
                        ]
                    ]
                ]
            },
            "id": 73,
            "name": "Prueba 1",
            "site": {
                "customer": {
                    "user": "fina",
                    "id": 1,
                    "name": "Test devel"
                },
                "id": 2,
                "name": "Dev - Triat"
            }
        },
        {
            "detail": "http://localhost:3000/building/54/",
            "bounds": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            2.2017,
                            41.4045
                        ],
                        [
                            2.202,
                            41.4047
                        ],
                        [
                            2.2022,
                            41.4046
                        ],
                        [
                            2.2019,
                            41.4044
                        ],
                        [
                            2.2017,
                            41.4045
                        ]
                    ]
                ]
            },
            "id": 54,
            "name": "Prueba 1",
            "site": {
                "customer": {
                    "user": "fina",
                    "id": 1,
                    "name": "Test devel"
                },
                "id": 2,
                "name": "Dev - Triat"
            }
        },
        {
            "detail": "http://localhost:3000/building/22/",
            "bounds": "None",
            "id": 22,
            "name": "Facultat de Lletres",
            "site": {
                "customer": {
                    "user": "fina",
                    "id": 1,
                    "name": "Test devel"
                },
                "id": 77,
                "name": "Barri Vell"
            }
        },
        {
            "detail": "http://localhost:3000/building/20/",
            "bounds": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            2.8271148550432286,
                            41.985945002581545
                        ],
                        [
                            2.8271148550432286,
                            41.986136730445374
                        ],
                        [
                            2.8278122293871006,
                            41.986136730445374
                        ],
                        [
                            2.8278122293871006,
                            41.985945002581545
                        ],
                        [
                            2.8271148550432286,
                            41.985945002581545
                        ]
                    ]
                ]
            },
            "id": 20,
            "name": "Rectorat",
            "site": {
                "customer": {
                    "user": "fina",
                    "id": 1,
                    "name": "Test devel"
                },
                "id": 77,
                "name": "Barri Vell"
            }
        },
        {
            "detail": "http://localhost:3000/building/37/",
            "bounds": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            2.2017,
                            41.4045
                        ],
                        [
                            2.202,
                            41.4047
                        ],
                        [
                            2.2022,
                            41.4046
                        ],
                        [
                            2.2019,
                            41.4044
                        ],
                        [
                            2.2017,
                            41.4045
                        ]
                    ]
                ]
            },
            "id": 37,
            "name": "Edifici test",
            "site": {
                "customer": {
                    "user": "fina",
                    "id": 1,
                    "name": "Test devel"
                },
                "id": 107,
                "name": "Area test"
            }
        },
        {
            "detail": "http://localhost:3000/building/47/",
            "bounds": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            2.1251917938171445,
                            41.488880879460716
                        ],
                        [
                            2.1251917938171445,
                            41.48925253441927
                        ],
                        [
                            2.125558511360248,
                            41.48925253441927
                        ],
                        [
                            2.125558511360248,
                            41.488880879460716
                        ],
                        [
                            2.1251917938171445,
                            41.488880879460716
                        ]
                    ]
                ]
            },
            "id": 47,
            "name": "Dev",
            "site": {
                "customer": {
                    "user": "fina",
                    "id": 1,
                    "name": "Test devel"
                },
                "id": 115,
                "name": "Dev"
            }
        }
    ],
    "page": 1,
    "next": "http://localhost:3000/building/?page=2"
}

我想用Python解析它并检索coordinates等于id的元素的53

import json
from pprint import pprint

target_id = 53

with open('data.json') as f:
    data = json.load(f)

data["results"][0]["id"]  # what should I do here to reach the record with `id` equal to 53?

1 个答案:

答案 0 :(得分:1)

我们将在此处使用列表理解。以下代码段将为您提供具有所需目标ID的所有元素

[x for x in data['results'] if x['id'] == target_id]