JArray获取元素值
#region 获取json getaqi
protected string getaqi()
{
string url = "http://www.pm25.in/api/querys/pm2_5.json?city=无锡&token=5j1znBVAsnSf5xQyNQyq";
string r = string.Empty;
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url.ToString());
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
//从Internet资源返回数据流
Stream webStream = webResponse.GetResponseStream();
//读取数据流
StreamReader webStreamReader = new StreamReader(webStream, System.Text.Encoding.UTF8);
//读取数据
r = webStreamReader.ReadToEnd();
webStreamReader.Close();
webStream.Close();
webResponse.Close();
return r;
}
#endregion
例如: 获取无锡某地PM2.5的值
[{"aqi":0,"area":"无锡","pm2_5":0,"pm2_5_24h":0,"position_name":"曹张","primary_pollutant":null,"quality":null,"station_code":"1190A","time_point":"2015-05-26T12:00:00Z"},{"aqi":67,"area":"无锡","pm2_5":28,"pm2_5_24h":33,"position_name":"漆塘","primary_pollutant":"颗粒物(PM10)","quality":"良","station_code":"1191A","time_point":"2015-05-26T12:00:00Z"},{"aqi":61,"area":"无锡","pm2_5":30,"pm2_5_24h":44,"position_name":"东亭","primary_pollutant":"颗粒物(PM10)","quality":"良","station_code":"1192A","time_point":"2015-05-26T12:00:00Z"},{"aqi":67,"area":"无锡","pm2_5":27,"pm2_5_24h":42,"position_name":"旺庄","primary_pollutant":"颗粒物(PM10)","quality":"良","station_code":"1193A","time_point":"2015-05-26T12:00:00Z"},{"aqi":71,"area":"无锡","pm2_5":32,"pm2_5_24h":52,"position_name":"堰桥","primary_pollutant":"颗粒物(PM10)","quality":"良","station_code":"1195A","time_point":"2015-05-26T12:00:00Z"},{"aqi":61,"area":"无锡","pm2_5":30,"pm2_5_24h":39,"position_name":"雪浪","primary_pollutant":"颗粒物(PM10)","quality":"良","station_code":"1188A","time_point":"2015-05-26T12:00:00Z"},{"aqi":70,"area":"无锡","pm2_5":42,"pm2_5_24h":46,"position_name":"黄巷","primary_pollutant":"颗粒物(PM10)","quality":"良","station_code":"1189A","time_point":"2015-05-26T12:00:00Z"},{"aqi":74,"area":"无锡","pm2_5":32,"pm2_5_24h":38,"position_name":"荣巷","primary_pollutant":"颗粒物(PM10)","quality":"良","station_code":"1194A","time_point":"2015-05-26T12:00:00Z"},{"aqi":68,"area":"无锡","pm2_5":31,"pm2_5_24h":42,"position_name":null,"primary_pollutant":"颗粒物(PM10)","quality":"良","station_code":null,"time_point":"2015-05-26T12:00:00Z"}]
获取 JArray 中某一个值如下:
JArray jo = (JArray)JsonConvert.DeserializeObject(getaqi());
string aqi = jo[1]["aqi"].ToString();
string quality = jo[1]["quality"].ToString();
结果:aqi="67"; quality="良";
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。