c# - 在Gridview中将Json数据抓取到字符串
问题描述
我正在设置一个网站 ASPX 文件,它可以获取接下来 3 天的天气,输出是 JSON 格式,它们的类太复杂了,我无法理解。如果我想只提取接下来 3 天的天气,我该怎么做呢?
这是顶部显示的 JSON 输出。
What I'm trying to accomplish is to extract the next 3 days of the weather "Description" and put it to a GridView.
我已经使用 JSON 美化器来获取类,但我仍然感到困惑。
任何帮助,将不胜感激
这是我当前的代码,我被困在这里
protected void Button1_Click(object sender, EventArgs e)
{
string searchTerm = TextBox1.Text;
var webRequest = (HttpWebRequest)WebRequest.Create("https://samples.openweathermap.org/data/2.5/forecast?q=" + Server.UrlEncode(searchTerm) + "&units=metric&APPID=b6907d289e10d714a6e88b30761fae22");
var webResponse = (HttpWebResponse)webRequest.GetResponse();
if (webResponse.StatusCode == HttpStatusCode.OK)
{
Label1.Text = "The Next 3 days of weather in your area in" + searchTerm + " .";
JavaScriptSerializer json = new JavaScriptSerializer();
StreamReader sr = new StreamReader(webResponse.GetResponseStream());
string resString = sr.ReadToEnd();
});
GridView1.DataSource = ?;
GridView1.DataBind();
}
else
Label.Text = "Invalid Response";
}
public class Main
{
public double temp { get; set; }
public double temp_min { get; set; }
public double temp_max { get; set; }
public double pressure { get; set; }
public double sea_level { get; set; }
public double grnd_level { get; set; }
public int humidity { get; set; }
public double temp_kf { get; set; }
}
public class Weather
{
public int id { get; set; }
public string main { get; set; }
public string description { get; set; }
public string icon { get; set; }
}
public class Clouds
{
public int all { get; set; }
}
public class Wind
{
public double speed { get; set; }
public double deg { get; set; }
}
public class Sys
{
public string pod { get; set; }
}
public class Rain
{
public double __invalid_name__3h { get;
set; }
}
public class Snow
{
public double __invalid_name__3h { get;
set; }
}
public class List
{
public int dt { get; set; }
public Main main { get; set; }
public List<Weather> weather { get; set; }
public Clouds clouds { get; set; }
public Wind wind { get; set; }
public Sys sys { get; set; }
public string dt_txt { get; set; }
public Rain rain { get; set; }
public Snow snow { get; set; }
}
public class Coord
{
public double lat { get; set; }
public double lon { get; set; }
}
public class City
{
public int id { get; set; }
public string name { get; set; }
public Coord coord { get; set; }
public string country { get; set; }
}
public class RootObject
{
public string cod { get; set; }
public double message { get; set; }
public int cnt { get; set; }
public List<List> list { get; set; }
public City city { get; set; }
}
解决方案
推荐阅读
- bash - bash SIGINT 陷阱触发一次但不再触发
- vue.js - 如何将动态参数添加到 graphql 查询中
- android - 约束布局在设备上将视图推送到屏幕外但不是 AVD
- excel - WebBrowser in userform blanks out when other objects are selected. 如何保持网站加载?
- xcode - 构建未显示在 App Store Connect 中 - 未收到任何状态电子邮件
- arm - 如何在运行时处理 SD 卡的移除和添加?
- java - 错误:java.lang.NoClassDefFoundError: org/apache/commons/collections/map/LRUMap
- elixir - Elixir - 在 Mac 上设置 MIX_ENV 会产生 FunctionClauseError
- postgresql - 关联表中的 gorm many2many 和附加字段
- excel - 将现有 Excel 工作簿中的特定表导入 Access