天氣JSON解析

2021-08-15 19:01:51 字數 1752 閱讀 6526

#coding:utf-8

import urllib2,json

url = "鄭州市

&output=json&ak=tuegdhcvwi6forqnlm0qmxxy9n0okoiq&callback=?"

response = urllib2.urlopen(url)

# 獲取服務端向前端響應的資料json_data_str,是乙個json字串。

json_data_str = response.read()

# 將json字串解析成字典物件

json_data_dict = json.loads(json_data_str)

# 獲取查詢日期

date = json_data_dict["date"]

print

'查詢日期:',date

# 獲取results鍵對應的值,該值是乙個list列表,並且列表的長度是1個元素。

result_list = json_data_dict["results"]

# 再從result_list列表中,取出第0個元素,取出的元素是乙個字典。

data_dict = result_list[0]

# data_dict中對應著4個鍵值對。

# 從data_dict字典中,獲取當前的城市以及pm25的值。

current_city_name = data_dict["currentcity"]

pm25 = data_dict["pm25"]

print

'當前城市:',current_city_name

print

'當前pm25值:',pm25

# 繼續獲取相關指數

tip_data_list = data_dict["index"]

# 遍歷tip_data_list指數列表

for tip_dict in tip_data_list:

title = tip_dict["title"]

zs = tip_dict["zs"]

tipt = tip_dict["tipt"]

des = tip_dict["des"]

print

'名稱:',title

print

'指數:',zs

print

'指數名稱:',tipt

print

'描述資訊:',des

# 繼續從data_dict中,獲取天氣列表

weather_data_list = data_dict["weather_data"]

for weather_dict in weather_data_list:

# 獲取日期

date = weather_dict["date"]

# 獲取天氣情況

weather = weather_dict["weather"]

# 獲取風力情況

wind = weather_dict["wind"]

# 獲取氣溫情況

temperature = weather_dict["temperature"]

print

''print

'日期:',date

print

'天氣:',weather

print

'風力:',wind

print

'溫度:',temperature

print

''

C 獲取天氣 JSON解析

winform獲取中國天氣的資料 中國天氣返回的是json資料格式,這裡做簡單的解析。用的獲取的天氣。101010100為城市 參見 介面詳解 由於c 不是asp.net,所以要在專案中要先新增兩個命名空間的引用。然後在專案中新增命名空間引用,如下 using system.web.script.s...

解析心知天氣API介面返回的json資料

json資料結構如下 北京今天和未來4天的預報 心知天氣json資料型別說明原址 daily last update 2015 09 20t18 00 00 08 00 資料更新時間 該城市的本地時間 解析如下 jsonobject jsonobject new jsonobject respons...

python用json解析爬取天氣預報資料

天氣預報 url 統一資源定位符 windows r cmd 開啟命令 輸入pip install requests 回車 import requests 引入python中內建的包 import json while 1 print 歡迎進入天氣查詢系統 city input 請輸入您要查詢的城市...