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"
}
]
}
Barmar
789k57 gold badges555 silver badges669 bronze badges
1 Answer 1
Try;
for item in fetch_data['geonames']:
print(item['lng'])
answered Feb 17, 2017 at 23:35
Tolgahan ÜZÜN
4337 silver badges14 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py
fetch_data, soitemis the key (e.g.'geonames'), which is a string, as the error message tells you.for key, item in fetch_data.enumerate():'dict' object has no attribute 'enumerate'...?the daily limit of 30000 credits for demo has been exceeded.