zoukankan      html  css  js  c++  java
  • 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']
    ))
     
  • 相关阅读:
    要成功先发疯
    情绪ABC理论
    树立和提高威信法
    javaagent
    sonar 使用
    sonar 代码质量管理
    四大思维工具,SWOT、PDCA、DISC、时间管理
    HyperLogLog
    位数组
    git checkout .和git checkout -f的区别;git add . git add -u git add -A的区别
  • 原文地址:https://www.cnblogs.com/vito13/p/7815013.html
Copyright © 2011-2022 走看看