1 public ActionResult Index(string city) 2 { 3 4 5 if (string.IsNullOrEmpty(city)) 6 { 7 city = "上海"; 8 } 9 //ak需要自己到百度地图官网申请 10 Uri weatherInfo = new Uri("http://api.map.baidu.com/telematics/v3/weather?location=" + city + "&output=json&ak=UKMGHnstHCOFzYBe2h70gi5fLsc0C0dG"); 11 12 HttpWebRequest request = WebRequest.Create(weatherInfo) as HttpWebRequest; 13 ListweatherDataList = new List (); 14 15 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) 16 { 17 // Get the response stream 18 StreamReader reader = new StreamReader(response.GetResponseStream()); 19 string json = reader.ReadToEnd(); 20 21 Weather model = JsonHelper.JsonToEntity (json); 22 List weatherDtoList = new List (); 23 List indexList = new List (); 24 25 foreach (WeatherResult result in model.Results) 26 { 27 indexList = result.Index; 28 weatherDataList = result.Weather_Data; 29 } 30 for (int i = 0; i < indexList.Count; i++) 31 { 32 WeatherDto weatherDto = new WeatherDto(); 33 weatherDto.Des = indexList[i].Des; 34 weatherDto.Tipt = indexList[i].Tipt; 35 weatherDto.Title = indexList[i].Title; 36 weatherDto.Zs = indexList[i].Zs; 37 38 if (i < weatherDataList.Count) 39 { 40 weatherDto.Date = weatherDataList[i].Date; 41 weatherDto.DayPictureUrl = weatherDataList[i].DayPictureUrl; 42 weatherDto.NightPictureUrl = weatherDataList[i].NightPictureUrl; 43 weatherDto.Temperature = weatherDataList[i].Temperature; 44 weatherDto.Weather = weatherDataList[i].Weather; 45 weatherDto.Wind = weatherDataList[i].Wind; 46 47 } 48 weatherDtoList.Add(weatherDto); 49 } 50 } 51 52 return View("NewView",weatherDataList); 53 } 54 55 } 56 57 public class WeatherDto 58 { 59 public string Title { get; set; } 60 61 public string Zs { get; set; } 62 63 public string Tipt { get; set; } 64 65 public string Des { get; set; } 66 67 public string Date { get; set; } 68 69 public string DayPictureUrl { get; set; } 70 71 public string NightPictureUrl { get; set; } 72 73 public string Weather { get; set; } 74 75 public string Wind { get; set; } 76 77 public string Temperature { get; set; } 78 } 79 80 81 public class Weather 82 { 83 public string Error { get; set; } 84 85 public string Status { get; set; } 86 87 public string Date { get; set; } 88 89 public List Results { get; set; } 90 } 91 92 public class WeatherResult 93 { 94 public string CurrentCity { get; set; } 95 96 public string Pm25 { get; set; } 97 98 public List Index { get; set; } 99 100 public List Weather_Data { get; set; } 101 }
1 @model IEnumerable2 3 @{ 4 Layout = null; 5 } 6 7 8 9 weather 10 "shortcut icon" href="http://p8.qhimg.com/t0158c24c5ddb3a6745.png" type="image/x-icon"> 11 "stylesheet" href="~/Content/css/Weather.css" /> 12 70 71 88 89 90 class="skin6" style="display: block;"> 9192 <select id="GetCity" οnchange="Change()"> 93 94 95 96 97 98 select> 99100class="morewether" id="partRefresh"> 101 102125 126"foreast"> 103
124- 104
108class="colora">@Model.ToList()[0].Date
105class="icon-tu">class="icon-qing">
106
@Model.ToList()[0].Weatherclass="otherinfo">@Model.ToList()[0].Temperature@Model.ToList()[0].Wind
107- 109
113class="colora">@Model.ToList()[1].Date
110class="icon-tu">class="icon-shachen">
111
@Model.ToList()[1].Weatherclass="otherinfo">@Model.ToList()[1].Temperature@Model.ToList()[1].Wind
112- 114
118class="colora">@Model.ToList()[2].Date
115class="icon-tu">class="icon-shachen">
116
@Model.ToList()[2].Weatherclass="otherinfo">@Model.ToList()[2].Temperature@Model.ToList()[2].Wind
117- 119
123class="colora">@Model.ToList()[3].Date
120class="icon-tu">class="icon-shachen">
121
@Model.ToList()[3].Weatherclass="otherinfo">@Model.ToList()[3].Temperature@Model.ToList()[3].Wind
122
效果图: