简体   繁体   中英

Parsing data JSON and Python

import requests

url = "http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo"
request = requests.get(url)
fetch_data = request.json()

print (request.content)

for item in fetch_data:
    print(item)

I am trying to fetch items within JSON Request geonames -> lng . If I make use of item['lng']

Then getting error

TypeError: string indices must be integers

EXAMPLE of JSON Request

{  
   "geonames":[  
      {  
         "lng":-99.12766456604,
         "geonameId":3530597,
         "countrycode":"MX",
         "name":"Mexiko-Stadt",
         "fclName":"city, village,...",
         "toponymName":"Mexico City",
         "fcodeName":"capital of a political entity",
         "wikipedia":"en.wikipedia.org/wiki/Mexico_City",
         "lat":19.428472427036,
         "fcl":"P",
         "population":12294193,
         "fcode":"PPLC"
      }
    ]
}

Try;

for item in fetch_data['geonames']:
    print(item['lng'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM