C# 将数据从API获取到SQL数据库
我在OpenWeatherMap中遇到了天气API问题。我将数据作为JSON对象获取。我可以在视图中显示它们,不幸的是,我不知道如何将它们存储到本地数据库中。我的目标是将它们存储在数据库中,以便在特定的模型中使用它们 我的模型:C# 将数据从API获取到SQL数据库,c#,asp.net-mvc,api,weather,C#,Asp.net Mvc,Api,Weather,我在OpenWeatherMap中遇到了天气API问题。我将数据作为JSON对象获取。我可以在视图中显示它们,不幸的是,我不知道如何将它们存储到本地数据库中。我的目标是将它们存储在数据库中,以便在特定的模型中使用它们 我的模型: public virtual Humidity Humidity { get; set; } public Object getWeatherForcast() { string url = "http://api.openweathermap
public virtual Humidity Humidity { get; set; }
public Object getWeatherForcast()
{
string url = "http://api.openweathermap.org/data/2.5/weather?q=Aalborg&APPID=MY APPID &units=imerial";
var client = new WebClient();
var content = client.DownloadString(url);
var serializer = new JavaScriptSerializer();
var jsonContent = serializer.Deserialize<Object>(content);
return jsonContent;
}
我的看法是:
<script>
$(document).ready(function () {
$.get("@Url.Action("GetWeather","Weather")", function (response) {
console.log(response);
$("#name").text(response.name);
$("#temp").text(response.main.temp);
$("#humidity").text(response.main.humidity);
});
});
如果有人能给我指出正确的方向,那会很有帮助的
非常感谢您具体遇到了什么问题?我不知道如何将Json对象中的特定数据保存到数据库中。我用下面的代码尝试它water.Wat=Json.response.main.湿度;db.Entry(water).State=EntityState.Modified;db.SaveChanges();`类型在给定上下文中无效
<script>
$(document).ready(function () {
$.get("@Url.Action("GetWeather","Weather")", function (response) {
console.log(response);
$("#name").text(response.name);
$("#temp").text(response.main.temp);
$("#humidity").text(response.main.humidity);
});
});
public ActionResult Edit([Bind(Include = "Id,Wat,TimeRec")] Water water)
{
if (ModelState.IsValid)
{
water.TimeRec = DateTime.Now;
//I think it should be done here
water.Wat = Json.response.main.humidity;
db.Entry(water).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Id = new SelectList(db.Plants, "Id", "Name", water.Id);
return View(water);
}