JArray获取元素值

MXS&Vincene  ─╄OvЁ  &0000003 ─╄OvЁ  MXS&Vincene 

MXS&Vincene  ─╄OvЁ:今天很残酷,明天更残酷,后天很美好,但是绝大部分人是死在明天晚上,只有那些真正的英雄才能见到后天的太阳。

MXS&Vincene  ─╄OvЁ:We're here to put a dent in the universe. Otherwise why else even be here? 

  

正文>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

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

原文地址:https://www.cnblogs.com/moxuanshang/p/4530436.html