【问题标题】:How to retrieve values from a nested json file? [duplicate]如何从嵌套的 json 文件中检索值? [复制]
【发布时间】:2019-12-02 01:14:33
【问题描述】:

我有一个嵌套的 json 文件,但我只想获得纬度、经度、名称和图像的值。如何更改此 json 文件的格式?

{"217417223": {
        "count": 1,
        "media_items": [
            {
                "image": "https://scontent.cdninstagram.com/vp/1c0099702809eb88225d205b2d4d9471/5DEA1FF2/t51.2885-15/sh0.08/e35/s640x640/49616199_376550376454921_2728948063380747681_n.jpg?_nc_ht=scontent.cdninstagram.com",
                "link": "https://www.instagram.com/p/BsAbzbNg5nJ/",
                "id": "1945677320190925257_1651881702"
            }
        ],
        "name": "Darlington railway station",
        "longitude": -1.5464,
        "additional_data": {
            "display_name": "Darlington, Park Lane, Bank Top, Darlington, North East England, England, DL1 5AE, United Kingdom",
            "place_id": "96959117",
            "lon": "-1.54675897913165",
            "boundingbox": [
                "54.5194926",
                "54.5223858",
                "-1.54771",
                "-1.5457426"
            ],
            "osm_type": "way",
            "osm_id": "68142725",
            "lat": "54.52105765",
            "address": {
                "town": "Darlington",
                "state_district": "North East England",
                "country": "United Kingdom",
                "county": "Darlington",
                "suburb": "Bank Top",
                "state": "England",
                "station": "Darlington",
                "postcode": "DL1 5AE",
                "country_code": "gb",
                "road": "Park Lane"
            }
        },
        "latitude": 54.5209,
        "id": 217417223
    },
    "394139011067403": {
        "count": 1,
        "media_items": [
            {
                "image": "https://scontent.cdninstagram.com/vp/3aac40c28cce8397d9fe565516de7502/5DE52C0B/t51.2885-15/sh0.08/e35/s640x640/46000756_291778651452164_4420788779216580668_n.jpg?_nc_ht=scontent.cdninstagram.com",
                "link": "https://www.instagram.com/p/BqYtsmxAECB/",
                "id": "1916482618820411521_1651881702"
            }
        ],
        "name": "George Street",
        "longitude": -3.26729,
        "additional_data": {
            "display_name": "5, Corstorphine Road, Corstorphine, Edinburgh, City of Edinburgh, Scotland, EH12 7AU, United Kingdom",
            "place_id": "50446446",
            "lon": "-3.2672375",
            "boundingbox": [
                "55.941973",
                "55.942173",
                "-3.2673375",
                "-3.2671375"
            ],
            "osm_type": "node",
            "osm_id": "3944885057",
            "lat": "55.942073",
            "address": {
                "city": "Edinburgh",
                "house_number": "5",
                "country": "United Kingdom",
                "county": "City of Edinburgh",
                "suburb": "Corstorphine",
                "state": "Scotland",
                "postcode": "EH12 7AU",
                "country_code": "gb",
                "road": "Corstorphine Road"
            }
        },
        "latitude": 55.94208,
        "id": 394139011067403
    }
}

我想要的文件是这样的:

[{
    "latitude": 54.5971,
    "name": "Belfast",
    "longitude": -5.9301,
    "id": 320007257,
    "img": "https://scontent.cdninstagram.com/vp/092066e2c0515b777420df6c5bda1b72/5DBCD171/t51.2885-15/e35/s320x320/47691939_1489011361230590_3361168451502130561_n.jpg?_nc_ht=scontent.cdninstagram.com",
    "link": "https://www.instagram.com/p/BrnHd_mgR9C/"
},{
    "latitude": 51.500955739612,
    "name": "Buckingham Palace",
    "longitude": -0.14312267303467,
    "id": 504822481,
    "img": "https://scontent.cdninstagram.com/vp/8982e1151375e7a1b273d894553de79e/5DC8A916/t51.2885-15/e35/s320x320/47586099_1814306185347470_7469652388588990014_n.jpg?_nc_ht=scontent.cdninstagram.com",
    "link": "https://www.instagram.com/p/Br0_bB6g2wq/"
}]

【问题讨论】:

  • 您应该(也)显示将从样本输入生成的输出。

标签: python json


【解决方案1】:
from json import loads

x = '''
{"217417223": { "count": 1, "media_items": [ { "image": "https://scontent.cdninstagram.com/vp/1c0099702809eb88225d205b2d4d9471/5DEA1FF2/t51.2885-15/sh0.08/e35/s640x640/49616199_376550376454921_2728948063380747681_n.jpg?_nc_ht=scontent.cdninstagram.com", "link": "https://www.instagram.com/p/BsAbzbNg5nJ/", "id": "1945677320190925257_1651881702" } ], "name": "Darlington railway station", "longitude": -1.5464, "additional_data": { "display_name": "Darlington, Park Lane, Bank Top, Darlington, North East England, England, DL1 5AE, United Kingdom", "place_id": "96959117", "lon": "-1.54675897913165", "boundingbox": [ "54.5194926", "54.5223858", "-1.54771", "-1.5457426" ], "osm_type": "way", "osm_id": "68142725", "lat": "54.52105765", "address": { "town": "Darlington", "state_district": "North East England", "country": "United Kingdom", "county": "Darlington", "suburb": "Bank Top", "state": "England", "station": "Darlington", "postcode": "DL1 5AE", "country_code": "gb", "road": "Park Lane" } }, "latitude": 54.5209, "id": 217417223 }, "394139011067403": { "count": 1, "media_items": [ { "image": "https://scontent.cdninstagram.com/vp/3aac40c28cce8397d9fe565516de7502/5DE52C0B/t51.2885-15/sh0.08/e35/s640x640/46000756_291778651452164_4420788779216580668_n.jpg?_nc_ht=scontent.cdninstagram.com", "link": "https://www.instagram.com/p/BqYtsmxAECB/", "id": "1916482618820411521_1651881702" } ], "name": "George Street", "longitude": -3.26729, "additional_data": { "display_name": "5, Corstorphine Road, Corstorphine, Edinburgh, City of Edinburgh, Scotland, EH12 7AU, United Kingdom", "place_id": "50446446", "lon": "-3.2672375", "boundingbox": [ "55.941973", "55.942173", "-3.2673375", "-3.2671375" ], "osm_type": "node", "osm_id": "3944885057", "lat": "55.942073", "address": { "city": "Edinburgh", "house_number": "5", "country": "United Kingdom", "county": "City of Edinburgh", "suburb": "Corstorphine", "state": "Scotland", "postcode": "EH12 7AU", "country_code": "gb", "road": "Corstorphine Road" } }, "latitude": 55.94208, "id": 394139011067403 } }
'''

j = loads(x.strip())

然后你可以使用例如j['217417223']['latitude'] 返回 54.5209 - 将其视为嵌套字典 - 然后您可以以相同的方式获取您的经度、名称、图像和任何您想要的字段。

【讨论】:

  • 感谢您的回复。但我的 json 文件来自 instagram 数据爬取。有太多的值,我无法在我的代码中输入像“217417223”这样的每个值......
  • @ClaireLiu - 在这种情况下,字典键 (j.keys()) 应该为您提供所有 id,例如 217417223
  • 对不起,我没明白。我是新手。我想在我的网页中显示位置。而且我不知道如何编写代码。 $.getJSON("mediatest.json", function(data) { // 循环遍历数据 $.each(data, function(key, data) { var myLatlng = new google.maps.LatLng(data.latitude, data.经度);
  • 建立一个网站是一个完全独立的问题,所以我会问一个新的问题。另外,您刚刚发布的示例代码似乎根本不是 Python...
猜你喜欢
  • 1970-01-01
  • 2019-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多