public FileResult saveToFile4days(int IdElement) { string path = @"C:\Univer\for4day.txt"; string file_type = "application/txt"; tempModel tempModel = TempStore.temps.Find(item => item.Id == IdElement); string json = JsonConvert.SerializeObject(tempModel); using (StreamWriter file = new StreamWriter(path)) { file.WriteLine(json); } return(File(path, file_type)); }
/// <summary> /// 详情页面 /// </summary> public tempModel XiangQing(string userId) { tempModel model = new tempModel(); string url = $"https://www.biomart.cn/japi/agency/enquiry/{userId}?_csrf=4c546c75-02af-4342-a3e4-4c7e4375fd9e&action=GetDetail&id={userId}"; var httpResponse = Util.WebRequestConstruct(url); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var result = streamReader.ReadToEnd(); Console.WriteLine(result); if (!string.IsNullOrEmpty(result)) { var dynamicObject = JsonConvert.DeserializeObject <dynamic>(result); if ((bool)dynamicObject.success) { model.phone = dynamicObject.results.phone != null ? dynamicObject.results.phone : ""; model.unit = dynamicObject.results.unit != null ? dynamicObject.results.unit : ""; model.email = dynamicObject.results.email != null ? dynamicObject.results.email : ""; model.searchLog = dynamicObject.results.searchLog != null?dynamicObject.results.searchLog.ToString().TrimEnd(']').TrimStart('[') : ""; model.productLog = dynamicObject.results.productLog != null?dynamicObject.results.productLog.ToString().TrimEnd(']').TrimStart('[') : ""; return(model); } else { return(model); } } else { // MessageBox.Show("没有获取到数据"); return(model); } } }
public ActionResult getWeather5days(string city5days) { string cc = city5days.Split(new char[] { ',' })[0]; string apiKey = "3ddbdbaa8509490ac43f89297c044421"; HttpWebRequest apiRequest = WebRequest.Create("http://api.openweathermap.org/data/2.5/forecast?q=" + cc + "&appid=" + apiKey) as HttpWebRequest; string apiResponse = ""; using (HttpWebResponse response = apiRequest.GetResponse() as HttpWebResponse) { StreamReader reader = new StreamReader(response.GetResponseStream()); apiResponse = reader.ReadToEnd(); } int cnt = Convert.ToInt32(JObject.Parse(apiResponse)["cnt"]); var o = JObject.Parse(apiResponse)["list"]; List <tempModel> allInfo = new List <tempModel>(); for (int i = 0; i < cnt; i++) { string w = Convert.ToString(o[i]); tempModel rootObject = JsonConvert.DeserializeObject <tempModel>(w); allInfo.Add(rootObject); } ViewBag.AllInfos = allInfo; int?time = null; List <tempModel> days = new List <tempModel>(); tempModel first_day = new tempModel(); first_day.weather.Add(new Weather()); int counterDays = 0; time = allInfo[0].dt_txt.Day; for (int i = 0; i < allInfo.Count; i++) { if (allInfo[i].dt_txt.Day == time) { counterDays++; first_day.clouds.all += allInfo[i].clouds.all; first_day.main.humidity += allInfo[i].main.humidity; first_day.main.pressure += allInfo[i].main.pressure; first_day.main.temp += allInfo[i].main.temp; first_day.weather[0].description = allInfo[i].weather[0].description; first_day.dt_txt = allInfo[i].dt_txt; first_day.wind.deg += allInfo[i].wind.deg; first_day.wind.speed += allInfo[i].wind.speed; } else { first_day.Id = i; first_day.clouds.all /= counterDays; first_day.main.humidity /= counterDays; first_day.main.pressure /= counterDays; first_day.main.temp /= counterDays; first_day.main.temp -= 273; first_day.wind.deg /= counterDays; first_day.wind.speed /= counterDays; days.Add(first_day); time = allInfo[i].dt_txt.Day; counterDays = 0; first_day = new tempModel(); first_day.weather.Add(new Weather()); } } TempStore.temps = days; ViewBag.averageWeather = days; return(PartialView("getWeather5days")); }