【发布时间】:2019-01-24 16:23:01
【问题描述】:
我从我的网络服务接收到这个 JSON 数据,现在的问题是我目前正试图检索方括号外的数据,因为我当前的代码只能检索 weather 数据。
每当我尝试从 coord 获取数据时,它都会不断返回值 0。
{
"coord":{
"lon":145.77,
"lat":-16.92
},
"weather":[
{
"id":802,
"main":"Clouds",
"description":"scattered clouds",
"icon":"03n"
}
],
"base":"stations",
"main":{
"temp":300.15,
"pressure":1007,
"humidity":74,
"temp_min":300.15,
"temp_max":300.15
},
"visibility":10000,
"wind":{
"speed":3.6,
"deg":160
},
"clouds":{
"all":40
},
"dt":1485790200,
"sys":{
"type":1,
"id":8166,
"message":0.2064,
"country":"AU",
"sunrise":1485720272,
"sunset":1485766550
},
"id":2172797,
"name":"Cairns",
"cod":200
}
这是我当前的代码,我正在尝试从大括号 coord 中获取数据,但它一直给出 0。任何帮助将不胜感激!
public partial class Book : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public class Result
{
public string id{ get; set; }
public string main{ get; set; }
public string description{ get; set; }
public string icon{ get; set; }
public decimal lon{ get; set; }
}
public class SearchList
{
public int resultCount;
public Result[] weather;
}
protected void Button1_Click(object sender, EventArgs e)
{
string searchTerm = TextBox1.Text;
var webRequest = (HttpWebRequest)WebRequest.Create
("http://samples.openweathermap.org/data/2.5/weather?id=2172797&appid=b6907d289e10d714a6e88b30761fae22");
var webResponse = (HttpWebResponse)webRequest.GetResponse();
if (webResponse.StatusCode == HttpStatusCode.OK)
{
JavaScriptSerializer json = new JavaScriptSerializer();
StreamReader sr = new StreamReader(webResponse.GetResponseStream());
string resString = sr.ReadToEnd();
SearchList list = json.Deserialize<SearchList>(resString);
GridView1.DataSource = list.weather;
GridView1.DataBind();
}
else
Label1.Text = "Invalid Response";
}
}
【问题讨论】:
-
更新
SearchList定义以包含其他所需数据 -
@Nkosi 我该怎么做你能告诉我吗?
标签: c# json web-services