2017-02-17 11 views
0
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) 

JSONリクエストgeonames -> lng内のアイテムを取得しようとしています。私はitem['lng']データの解析JSONとPython

を利用する場合は、JSONリクエスト

{ 
    "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" 
     } 
    ] 
} 
+0

あなたは*辞書* 'fetch_data'を反復処理しているので、エラーメッセージがわかりますよう' item'は、文字列であるキー(例えば ' 'geonames'')、です。 – jonrsharpe

+0

fetch_data.enumerate()の 'for key、item:' – Barmar

+1

@Barmar ''dict'オブジェクトに 'enumerate'という属性がありません...? –

答えて

2

てみのエラー

TypeError: string indices must be integers 

例を得ます。

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

ありがとう!それは素晴らしい仕事をした – Kris1511