처음 응답은 그리 좋지 않았습니다. 우리 중 일부는 원시 JSON을 간과했기 때문입니다. 그러나 은을 제공 했으므로 앞으로 더 자세하고 유용한 스 니펫을 제공하는 것이 더 낫습니다. 말했다
, 나는 사전에 데이터를로드하고 같은 일을 할 것입니다 :
import json
json_string = """{
"response": {
"version":"0.1",
"termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"conditions": 1
}
, "results": [
{
"name": "London",
"city": "London",
"state": "AR",
"country": "US",
"country_iso3166":"US",
"country_name":"USA",
"zmw": "72847.1.99999",
"l": "https://stackoverflow.com/q/zmw:72847.1.99999"
}
,
{
"name": "London",
"city": "London",
"state": "KY",
"country": "US",
"country_iso3166":"US",
"country_name":"USA",
"zmw": "40741.1.99999",
"l": "https://stackoverflow.com/q/zmw:40741.1.99999"
}
,
{
"name": "London",
"city": "London",
"state": "MN",
"country": "US",
"country_iso3166":"US",
"country_name":"USA",
"zmw": "56036.3.99999",
"l": "https://stackoverflow.com/q/zmw:56036.3.99999"
}
,
{
"name": "London",
"city": "London",
"state": "OH",
"country": "US",
"country_iso3166":"US",
"country_name":"USA",
"zmw": "43140.1.99999",
"l": "https://stackoverflow.com/q/zmw:43140.1.99999"
}
,
{
"name": "London",
"city": "London",
"state": "ON",
"country": "CA",
"country_iso3166":"CA",
"country_name":"Canada",
"zmw": "00000.1.71623",
"l": "https://stackoverflow.com/q/zmw:00000.1.71623"
}
,
{
"name": "London",
"city": "London",
"state": "TX",
"country": "US",
"country_iso3166":"US",
"country_name":"USA",
"zmw": "76854.1.99999",
"l": "https://stackoverflow.com/q/zmw:76854.1.99999"
}
,
{
"name": "London",
"city": "London",
"state": "",
"country": "UK",
"country_iso3166":"GB",
"country_name":"United Kingdom",
"zmw": "00000.1.03772",
"l": "https://stackoverflow.com/q/zmw:00000.1.03772"
}
,
{
"name": "London",
"city": "London",
"state": "WV",
"country": "US",
"country_iso3166":"US",
"country_name":"USA",
"zmw": "25126.1.99999",
"l": "https://stackoverflow.com/q/zmw:25126.1.99999"
}
]
}
}"""
json_object = json.loads(json_string)
world_dict = {}
for item in json_object['response']['results']:
item_country = item['country']
in_dict = world_dict.get(item_country)
if in_dict:
world_dict[item_country].extend([item])
else:
world_dict[item_country] = [item]
country = raw_input('Country: ')
response = world_dict.get(country)
if response:
for item in response:
print item
else:
print "Not a valid country"
편집 : JSON 문자열이 아닌 URL을 사용하여 의견을 바탕으로 .
import requests
url = 'http://api.wunderground.com/api/a8c3e5ce8970ae66/conditions/q/London.json'
data = requests.get(url).json()
world_dict = {}
for item in data['response']['results']:
item_country = item['country']
in_dict = world_dict.get(item_country)
if in_dict:
world_dict[item_country].extend([item])
else:
world_dict[item_country] = [item]
country = raw_input('Country: ')
response = world_dict.get(country)
if response:
for item in response:
print item
else:
print "Not a valid country"
'json' 모듈을 사용하여 JSON을 구문 분석하는 방법을 알고 있습니까? – FamousJameous
@FamousJameous 흠, 아니, 내가 추측 하건대. – ThatOnePythonNoob
다음 링크는 나중에 참조 할 수 있습니다. https://docs.python.org/2.7/library/json.html – FamousJameous