我有一个非常大的.json
转换为string
,其中包含多个城市/国家/地区。
我想根据用户选择的国家/地区提取城市信息(伦敦只是一个例子)。
例如,如果用户输入的Country
为:UK
,则会从字符串中提取以下信息:
由于我的经验不足,我不太确定如何实现这一目标,但我知道这需要if声明。到目前为止我的进展:
Country = raw_input('Country: ')
if 'UK' in string:
???
答案 0 :(得分:1)
最初的反应并不是很好,因为我们中的一些人忽略了原始的JSON。但是, 提供了它,以便将来更好地表明您展示的代码段有更完整(和有效)的对应部分。
那就是说,我会将数据加载到字典中并执行以下操作:
if (element.equals("air")) {
System.out.println("the time is " + time1);
} else if (element.equals("water"))
System.out.println("the time is " + time2);
else if (element.equals("steel"))
System.out.println(time3);
编辑: 根据注释使用URL而不是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": "/q/zmw:72847.1.99999"
}
,
{
"name": "London",
"city": "London",
"state": "KY",
"country": "US",
"country_iso3166":"US",
"country_name":"USA",
"zmw": "40741.1.99999",
"l": "/q/zmw:40741.1.99999"
}
,
{
"name": "London",
"city": "London",
"state": "MN",
"country": "US",
"country_iso3166":"US",
"country_name":"USA",
"zmw": "56036.3.99999",
"l": "/q/zmw:56036.3.99999"
}
,
{
"name": "London",
"city": "London",
"state": "OH",
"country": "US",
"country_iso3166":"US",
"country_name":"USA",
"zmw": "43140.1.99999",
"l": "/q/zmw:43140.1.99999"
}
,
{
"name": "London",
"city": "London",
"state": "ON",
"country": "CA",
"country_iso3166":"CA",
"country_name":"Canada",
"zmw": "00000.1.71623",
"l": "/q/zmw:00000.1.71623"
}
,
{
"name": "London",
"city": "London",
"state": "TX",
"country": "US",
"country_iso3166":"US",
"country_name":"USA",
"zmw": "76854.1.99999",
"l": "/q/zmw:76854.1.99999"
}
,
{
"name": "London",
"city": "London",
"state": "",
"country": "UK",
"country_iso3166":"GB",
"country_name":"United Kingdom",
"zmw": "00000.1.03772",
"l": "/q/zmw:00000.1.03772"
}
,
{
"name": "London",
"city": "London",
"state": "WV",
"country": "US",
"country_iso3166":"US",
"country_name":"USA",
"zmw": "25126.1.99999",
"l": "/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"
答案 1 :(得分:0)
import json
country = raw_input('Country: ')
jsondata = "the large json string mentioned in your post"
info = json.loads(jsondata)
for item in info:
if item['country'] == country:
print item
答案 2 :(得分:0)
你可以试试这个。可能仍然要考虑代码中的一些用户输入错误。例如,str.strip()和大写敏感。
import json
input_country = raw_input('Please enter country:')
with open('London.json') as fp:
london_json = fp.read()
london = json.loads(london_json)
for item in london["response"]["results"]:
if item['country'] == input_country:
print json.dumps(item, indent = 2)