Python:Json响应解析

时间:2020-01-07 16:28:37

标签: python json

我有一个google API响应,但是我无法解决如何在python的JSON响应中到达“ formatted_address”的问题。 我尝试过:

import json
with open('test.json', mode='r', encoding='utf-8') as f:
  data = json.load(f)

json_rep = data["results"]
print(json_rep)
aa = json.load(json_rep)
aaa = aa["formatted_address"]
print(aaa)

我需要获取为-"formatted_address" : "M. Sleževičiaus g. 7, Vilnius 06326, Lithuania",

的formatted_address值

JSON响应:

{
   "plus_code" : {
      "compound_code" : "P7Q2+3M Vilnius, Lithuania",
      "global_code" : "9G67P7Q2+3M"
   },
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "7",
               "short_name" : "7",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "M. Sleževičiaus gatvė",
               "short_name" : "M. Sleževičiaus g.",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Vilnius",
               "short_name" : "Vilnius",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Vilniaus miesto savivaldybė",
               "short_name" : "Vilniaus m. sav.",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Vilniaus apskritis",
               "short_name" : "Vilniaus apskr.",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Lithuania",
               "short_name" : "LT",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "06326",
               "short_name" : "06326",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "M. Sleževičiaus g. 7, Vilnius 06326, Lithuania",
         "geometry" : {
            "location" : {
               "lat" : 54.73771790000001,
               "lng" : 25.2518586
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 54.73906688029151,
                  "lng" : 25.2532075802915
               },
               "southwest" : {
                  "lat" : 54.73636891970851,
                  "lng" : 25.2505096197085
               }
            }
         },
         "place_id" : "ChIJC8ibiKGR3UYRZyClqH26vYE",
         "plus_code" : {
            "compound_code" : "P7Q2+3P Vilnius, Lithuania",
            "global_code" : "9G67P7Q2+3P"
         },
         "types" : [ "establishment", "point_of_interest" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "7",
               "short_name" : "7",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "M. Sleževičiaus gatvė",
               "short_name" : "M. Sleževičiaus g.",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Vilnius",
               "short_name" : "Vilnius",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Vilniaus miesto savivaldybė",
               "short_name" : "Vilniaus m. sav.",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Vilniaus apskritis",
               "short_name" : "Vilniaus apskr.",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Lithuania",
               "short_name" : "LT",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "06326",
               "short_name" : "06326",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "M. Sleževičiaus g. 7, Vilnius 06326, Lithuania",
         "geometry" : {
            "location" : {
               "lat" : 54.737146,
               "lng" : 25.252066
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 54.7384949802915,
                  "lng" : 25.2534149802915
               },
               "southwest" : {
                  "lat" : 54.7357970197085,
                  "lng" : 25.2507170197085
               }
            }
         },
         "place_id" : "ChIJIxUZgxGR3UYR6ez4mTMiSLQ",
         "plus_code" : {
            "compound_code" : "P7P2+VR Vilnius, Lithuania",
            "global_code" : "9G67P7P2+VR"
         },
         "types" : [ "street_address" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Unnamed Road",
               "short_name" : "Unnamed Road",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Vilnius",
               "short_name" : "Vilnius",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Vilniaus miesto savivaldybė",
               "short_name" : "Vilniaus m. sav.",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Vilniaus apskritis",
               "short_name" : "Vilniaus apskr.",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Lithuania",
               "short_name" : "LT",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "06326",
               "short_name" : "06326",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Unnamed Road, Vilnius 06326, Lithuania",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 54.7377477,
                  "lng" : 25.2520756
               },
               "southwest" : {
                  "lat" : 54.73754590000001,
                  "lng" : 25.2512685
               }
            },
            "location" : {
               "lat" : 54.7376486,
               "lng" : 25.2516736
            },
            "location_type" : "GEOMETRIC_CENTER",
            "viewport" : {
               "northeast" : {
                  "lat" : 54.7389957802915,
                  "lng" : 25.2530210302915
               },
               "southwest" : {
                  "lat" : 54.7362978197085,
                  "lng" : 25.2503230697085
               }
            }
         },
         "place_id" : "ChIJ4WCnkBGR3UYRWLeQUpD2P9w",
         "types" : [ "route" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "06326",
               "short_name" : "06326",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Vilnius",
               "short_name" : "Vilnius",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Vilnius city municipality",
               "short_name" : "Vilnius city municipality",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Vilnius County",
               "short_name" : "Vilnius County",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Lithuania",
               "short_name" : "LT",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Vilnius 06326, Lithuania",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 54.739305,
                  "lng" : 25.2588261
               },
               "southwest" : {
                  "lat" : 54.733427,
                  "lng" : 25.2476399
               }
            },
            "location" : {
               "lat" : 54.7364821,
               "lng" : 25.2555399
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 54.739305,
                  "lng" : 25.2588261
               },
               "southwest" : {
                  "lat" : 54.733427,
                  "lng" : 25.2476399
               }
            }
         },
         "place_id" : "ChIJWUBH5BOR3UYR4paeLozRABM",
         "types" : [ "postal_code" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Fabijoniškės",
               "short_name" : "Fabijoniškės",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Vilnius",
               "short_name" : "Vilnius",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Vilnius city municipality",
               "short_name" : "Vilnius city municipality",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Vilnius County",
               "short_name" : "Vilnius County",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Lithuania",
               "short_name" : "LT",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Fabijoniškės, Vilnius, Lithuania",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 54.7576305,
                  "lng" : 25.2703037
               },
               "southwest" : {
                  "lat" : 54.7194219,
                  "lng" : 25.214688
               }
            },
            "location" : {
               "lat" : 54.73416659999999,
               "lng" : 25.2494496
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 54.7576305,
                  "lng" : 25.2703037
               },
               "southwest" : {
                  "lat" : 54.7194219,
                  "lng" : 25.214688
               }
            }
         },
         "place_id" : "ChIJqzx2KAyR3UYRyei9NObcm4Q",
         "types" : [ "administrative_area_level_3", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Fabijoniškės",
               "short_name" : "Fabijoniškės",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Vilnius",
               "short_name" : "Vilnius",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Vilnius city municipality",
               "short_name" : "Vilnius city municipality",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Vilnius County",
               "short_name" : "Vilnius County",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Lithuania",
               "short_name" : "LT",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Fabijoniškės, Vilnius, Lithuania",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 54.7588229,
                  "lng" : 25.270351
               },
               "southwest" : {
                  "lat" : 54.72018689999999,
                  "lng" : 25.214302
               }
            },
            "location" : {
               "lat" : 54.7352291,
               "lng" : 25.2467146
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 54.7588229,
                  "lng" : 25.270351
               },
               "southwest" : {
                  "lat" : 54.72018689999999,
                  "lng" : 25.214302
               }
            }
         },
         "place_id" : "ChIJbWDoSAyR3UYR4UZFTI3RACY",
         "types" : [ "neighborhood", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Vilnius",
               "short_name" : "Vilnius",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Vilnius city municipality",
               "short_name" : "Vilnius city municipality",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Vilnius County",
               "short_name" : "Vilnius County",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Lithuania",
               "short_name" : "LT",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Vilnius, Lithuania",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 54.832544,
                  "lng" : 25.4814883
               },
               "southwest" : {
                  "lat" : 54.567798,
                  "lng" : 25.024376
               }
            },
            "location" : {
               "lat" : 54.6871555,
               "lng" : 25.2796514
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 54.832544,
                  "lng" : 25.4814883
               },
               "southwest" : {
                  "lat" : 54.567798,
                  "lng" : 25.024376
               }
            }
         },
         "place_id" : "ChIJ9QhkXPuT3UYRQNzpcIzRAAQ",
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Vilnius city municipality",
               "short_name" : "Vilnius city municipality",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Vilnius County",
               "short_name" : "Vilnius County",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Lithuania",
               "short_name" : "LT",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Vilnius city municipality, Lithuania",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 54.832544,
                  "lng" : 25.4814883
               },
               "southwest" : {
                  "lat" : 54.567798,
                  "lng" : 25.024376
               }
            },
            "location" : {
               "lat" : 54.6710761,
               "lng" : 25.2878721
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 54.832544,
                  "lng" : 25.4814883
               },
               "southwest" : {
                  "lat" : 54.567798,
                  "lng" : 25.024376
               }
            }
         },
         "place_id" : "ChIJ7epZQvuT3UYRg4dk0tyVXHU",
         "types" : [ "administrative_area_level_2", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Vilnius County",
               "short_name" : "Vilnius County",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Lithuania",
               "short_name" : "LT",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Vilnius County, Lithuania",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 55.5174369,
                  "lng" : 26.760213
               },
               "southwest" : {
                  "lat" : 54.1276583,
                  "lng" : 24.386375
               }
            },
            "location" : {
               "lat" : 54.8086502,
               "lng" : 25.2182139
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 55.5174369,
                  "lng" : 26.760213
               },
               "southwest" : {
                  "lat" : 54.1276583,
                  "lng" : 24.386375
               }
            }
         },
         "place_id" : "ChIJH4KkkAKi3UYRsNjpcIzRAAM",
         "types" : [ "administrative_area_level_1", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Lithuania",
               "short_name" : "LT",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Lithuania",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 56.45032089999999,
                  "lng" : 26.835523
               },
               "southwest" : {
                  "lat" : 53.8967949,
                  "lng" : 20.931
               }
            },
            "location" : {
               "lat" : 55.169438,
               "lng" : 23.881275
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 56.45032089999999,
                  "lng" : 26.835523
               },
               "southwest" : {
                  "lat" : 53.8967949,
                  "lng" : 20.931
               }
            }
         },
         "place_id" : "ChIJE74zDxSU3UYRubpdpdNUCvM",
         "types" : [ "country", "political" ]
      }
   ],
   "status" : "OK"
}

1 个答案:

答案 0 :(得分:1)

您不需要多次调用json.load。第一次调用将把整个json结构解析为一个嵌套字典。因此,您的示例应变为:

import json
with open('test.json', mode='r', encoding='utf-8') as f:
      data = json.load(f)

json_rep = data["results"]
print(json_rep)
aa = json_rep["formatted_address"]
print(aa)