码迷,mamicode.com
首页 > 编程语言 > 详细

JArray获取元素值

时间:2015-05-26 14:22:42      阅读:1400      评论:0      收藏:0      [点我收藏+]

标签:

#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="良";

 

JArray获取元素值

标签:

原文地址:http://www.cnblogs.com/Vincene/p/4530436.html

(2)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!