• Python3-笔记-E-012-库-JSON


    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']
    ))
     
  • 相关阅读:
    <!--[if IE 9]>....<!end if-->
    背景颜色设置
    UIImageView 响应UIButton的点击事件
    响应键盘return事件
    iOS 封装数据请求,解析数据(异步)
    UIImageView 圆角
    JS 控制文本框必须输入值才能查询
    MVC4 @Html.Raw()
    JS加成显示
    随机生成人物名称
  • 原文地址:https://www.cnblogs.com/vito13/p/7815013.html
Copyright © 2020-2023  润新知