私たちのうちのいくつかが未加工の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"
EDIT: むしろ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 Hmm、いいえ、私は一見を持っているでしょう。 – ThatOnePythonNoob
今後の参考のためのリンクは次のとおりです。https://docs.python.org/2.7/library/json.html – FamousJameous