import json, pprint
# 支持的类型:字典、列表、整型、浮点型、字符串、布尔型或None
def LoadsAndDumps():
stringOfJsonData = '{"name": "Zophie", "isCat": true, "miceCaught": 0, "felineIQ": null}'
# 返回字典
data = json.loads(stringOfJsonData)
pprint.pprint(data)
# 返回json
jsondata = json.dumps(data)
print(jsondata)
# 测试读取天气预报
import requests, pprint, json
def fetchWeathernow(location):
result = requests.get('https://api.seniverse.com/v3/weather/now.json?', params={
'key': 'xo4gfeq107zexjat',
'location': location,
'language': 'zh-Hans',
'unit': 'c',
}, timeout=1)
return result.text
def fetchWeatherdaily(location):
result = requests.get('https://api.seniverse.com/v3/weather/daily.json?', params={
'key': 'xo4gfeq107zexjat',
'location': location,
'language': 'zh-Hans',
'unit': 'c',
'start': 0,
'days': 5
}, timeout=1)
return result.text
if __name__ == '__main__':
location = 'tianjin'
result = fetchWeathernow(location)
datanow = json.loads(result)
now = datanow['results'][0]['now']
city = datanow['results'][0]['location']['name']
print('%s现在的天气是%s %s摄氏度' % (city, now['text'], now['temperature']))
result = fetchWeatherdaily(location)
datadaily = json.loads(result)
lst = datadaily['results'][0]['daily']
city = datadaily['results'][0]['location']['name']
print('%s近三天的天气' % city)
for m in lst:
print('%s 白天:%s,夜晚:%s,最高温度:%s,最低温度:%s,风向:%s,风力:%s' %
(m['date'],
m['text_day'],
m['text_night'],
m['high'],
m['low'],
m['wind_direction'],
m['wind_scale']
))